aspose file tools*
The moose likes Java in General and the fly likes How to call EXE from JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to call EXE from JAVA" Watch "How to call EXE from JAVA" New topic
Author

How to call EXE from JAVA

narayana murthy pedapudi
Ranch Hand

Joined: Jul 23, 2003
Posts: 42
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

Joined: Jul 02, 2001
Posts: 118
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

Joined: Jul 23, 2003
Posts: 42
Hi Peter,
Thanks for the Information.
Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
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 ]

Tiago Fernandez
http://www.tiago182.spyw.com/
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
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

Joined: Aug 04, 2003
Posts: 175
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() );
}
 
 
subject: How to call EXE from JAVA