How to run an exe file using java code?The .exe file is already there. The plan is to write a Java code for running the same. Any tutorial or reference for the same?
Asked
Active
Viewed 9,600 times
0
-
5What have the Googles told you? – Chris Eberle Dec 23 '11 at 06:29
-
Does that .exe needs STDIN/STDOUT? Is it a GUI app? Do you need to be notified when it terminates? Do you want it to continue running after your Java program terminates? – Thilo Dec 23 '11 at 06:33
4 Answers
5
Try the following code:
try
{
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("Program.exe") ;
InputStream in = p.getInputStream() ;
OutputStream out = p.getOutputStream ();
InputStream err = p.getErrorStream() ;
//do whatever you want
p.destroy() ;
}
catch(Exception exc)
{
/*handle exception*/
}
Tyler Moore
- 58
- 6
Raoul George
- 2,805
- 1
- 20
- 25
2
You need to execute exec() method of Runtime that returns Process instance or use ProcessBuilder class methods.
Process process=Runtime.getRuntime().exec("file.exe");
KV Prajapati
- 92,042
- 19
- 143
- 183
0
The quickest and easiest way is just to do as follows:
Runtime.getRuntime().exec("yourapp.exe");
Also, see an alternative approach at http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
There is an example there. ProcessBuilder gives you a bit better control over the process and arguments and is probably a bit cleaner and more expressive, particularly if you need to supply arguments, but does result in a few more lines of code.
squawknull
- 5,111
- 2
- 15
- 26
0
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
Orn Kristjansson
- 3,467
- 4
- 25
- 38