• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

catching an exception thrown by a commandline app.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

This might not be appropriate for an advanced category, but never the less I'm trying my luck here.

See, I have this problem where I call a commandline application (a windows .exe) from my java app:


//commands, and arguments
list = new ArrayList<String>();
list.add("myCmd");
processBuilder = new ProcessBuilder(list);
processBuilder.directory(someDir);
process = processBuilder.start();

This is all fine and dandy. However, the commandline app. sometimes throws an exception which bubbles all the way up to OS level resulting in an alert box, and pausing execution.

Question: how to catch an exception thrown by a commandline app. which is invoked from a Java app. from within that same Java app.

Any help on this 'issue' would be much appreciated.

cheers

-michael
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't catch an exception in an external process. Not with pure Java anyway. Maybe an individual O.S. may provide APIs for running programs with more control over error handling, but you'd have to access that with JNI.

If the program you are running is in fact a Java application, then you could gain more control by running it within the same JVM, instead of as an external process. Instead of invoking its executable, make its classes available to some or other ClassLoader, and invoke its main() method. You can try...catch around that invocation and hence catch any Java exception.
 
Michael Hansen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspected as much.

Thank you Peter!
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic