Two Laptop Bag*
The moose likes Java in General and the fly likes Calling an 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 "Calling an exe from java" Watch "Calling an exe from java" New topic
Author

Calling an exe from java

Mary White
Ranch Hand

Joined: Jan 27, 2003
Posts: 41
I'm writing an application that need to call a PowerBuilder executable.
How can I do this?
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
Hi,
Yes from a standalone java program you should be able to do this through use of the java.lang.Runtime object exec method. Look it up in the api and play around with it. I've used it successfully to run dos commands. If the command to launch powberbuilder app is complex, but it in a bat file and execute the bat file. If you can't figure the api out, let me know and try to dig up a code sample.
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
Hi Mary,
I dug up a code sample:
String command = "c:\\acodetest\\mybat.bat";
Process process = null;
int exitVal = -10;
try {
process = Runtime.getRuntime().exec(command);
process.waitFor();
exitVal = process.exitValue();
} catch (InterruptedException ie) {
System.out.println(ie.getMessage());
} catch (java.io.IOException ioe) {
System.out.println(ioe.getMessage());
}
System.out.println("Ran mybat - return value was " + exitVal);
the bat file contents are:
cd c:\acodetest
dir > a.out
You'll know if it works, because a file, a.out will be produced. (make sure c:\acodetest is where the bat file is - or change the sample code.)
Hope this helps.
- Lu Battist
Mary White
Ranch Hand

Joined: Jan 27, 2003
Posts: 41
Hi Lu,
Thanks for your answer. It's veru helpful.
Can I use the same procedure to run the executable from a java web application, having the executable sitting on the server?
My application is a JSP / Servlets solution.
Thanks
Mary.
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
Hi Mary,
Yes you can do this from a JSP/Servlet but you do need to set up a security policy to allow for it. I've done this before but don't remember the specifics. J2EE or JDK doc should cover it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling an exe from java