Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

process.start

  Asked By: Nisha    Date: Aug 06    Category: Java    Views: 1615
  

in c# there is System.
System.Diagnostics.Process.Start("f.exe","abc");

this excute the program "f.exe" and pass paramters to it which is "abc"

can i do that with java

and how??

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Adalwin Fischer     Answered 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;
}

}

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




Tagged: