Running a .exe file using Java

爷,独闯天下 提交于 2019-11-28 00:37:09
Raoul George

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*/
}

You need to execute exec() method of Runtime that returns Process instance or use ProcessBuilder class methods.

Process process=Runtime.getRuntime().exec("file.exe");

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.

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("javac");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!