Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Nisha Gor   on Aug 06 In Java Category.

  
Question Answered By: Adalwin Fischer   on Aug 06

To run an exe, try this

import java.io.*;



public class Runner {

public static void main(String args[]) {

String s =Runner.runIt("whatever.exe","argument_here");

if(s.equals(""))
s="NO OUTPUT FROM EXE";
System.out.println("Output :\n"+s);

}


static String output=new String();

public static String runIt(String jdk, String file)
{

try{
Runtime rt = Runtime.getRuntime();

final Process p = rt.exec(jdk+" "+file);
Thread t = new Thread(new Runnable() {
public void run() {
try {
BufferedReader br_in = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String buff = null;
while ((buff = br_in.readLine()) != null) {
// System.out.println("Process out :" + buff);
output+=buff;
try {Thread.sleep(100); } catch(Exception e) {}
}
br_in.close();
} catch (IOException ioe) {
System.out.println("Exception caught printing javac
result");
ioe.printStackTrace();
}
}
});
t.start();

Thread r= new Thread(new Runnable() {
public void run() {
try {
BufferedReader br_err = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String buff = null;
while ((buff = br_err.readLine()) != null) {
// System.out.println(buff);
output+=buff;

try {Thread.sleep(100); } catch(Exception e) {}
}
br_err.close();
} catch (IOException ioe) {
System.out.println("Exception caught printing javac
result");
ioe.printStackTrace();
}
}
});
r.start();


t.join();
r.join();

} catch (Exception e) {
System.out.println("Exception");
e.printStackTrace();
}

return output;
}

}

Share: 

 
 
Didn't find what you were looking for? Find more on process.start Or get search suggestion and latest updates.


Tagged: