• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call EXE from JAVA

 
narayana murthy pedapudi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Buddies,
I have an Exe File, want to call that in my java application can anyone tell me how to do it.
narayana
 
Peter Kristensson
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Take a look at the java.lang.Runtime.exec() method:
http://java.sun.com/j2se/1.4.1/docs/api/java/lang/Runtime.html
/Peter
 
narayana murthy pedapudi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Thanks for the Information.
 
Tiago Fernandez
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class ExeCommand {
public static void main(String args[]) {
try {
Process p = Runtime.getRuntime().exec("path_of_your_exe_file");
p.waitFor();
}
catch(IOException e){}
catch(InterruptedException e){}
}
}
[ August 01, 2003: Message edited by: Tiago Fernandez ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiago - it is not quite that simple. IF the application writes anything to stdout or stderr you need to provide for reading those streams or you will get strange behavior.
Bill
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use java.lang.Runtime.exec() for executing external commands. If you are expecting any output from that application, use InputStream on that created Process.
Here is the sample code for DIR
*******************************
Runtime runtime = Runtime.getRuntime();
try
{
Process p1 = runtime.exec("cmd /c Dir");
InputStream is = p1.getInputStream();
int i = 0;
while( (i = is.read() ) != -1)
{
System.out.print((char)i);
}

}
catch(IOException ioException)
{
System.out.println(ioException.getMessage() );
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic