This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling an exe from java

 
Mary White
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing an application that need to call a PowerBuilder executable.
How can I do this?
 
Lu Battist
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic