• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get exception when a batch file fails to do its intended Task.

 
Ranch Hand
Posts: 35
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I'm using batch files to execute multiple build.xml files. However, there is always a possibility of getting failure in build file execution :-)
So when such error occurs how can I terminate the java program execution, because correct execution of all build files is the prerequisite for further Program execution?

can anyone please help?

Attaching my code...

 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe instead of running the scripts through a batch file you could use the Ant API to invoke the builds. That would give you greater control over such matters.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ulf's is a good suggestion, but in case you don't want to do that (or others stumble upon this question and want to know the answer):

java.lang.Runtime.exec returns a java.lang.Process. There are methods in that class to let you know the return code of the script that you ran. You can also get hold of the standard error out and standard error of the script so you can see if it prints out a message.

As always when dealing with this, read When Runtime.exec won't
 
Ranjeet Deshmukh
Ranch Hand
Posts: 35
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote:Ulf's is a good suggestion, but in case you don't want to do that (or others stumble upon this question and want to know the answer):

java.lang.Runtime.exec returns a java.lang.Process. There are methods in that class to let you know the return code of the script that you ran. You can also get hold of the standard error out and standard error of the script so you can see if it prints out a message.

As always when dealing with this, read When Runtime.exec won't




Hi Mike,

Thanks a lot for the detailed explanation. It worked fine for me.
Attaching modified code...

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avoid System.exit. It can be dangerously vicious, stopping things in midstream. I suspect it is unnecessary there.
 
Ranjeet Deshmukh
Ranch Hand
Posts: 35
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Avoid System.exit. It can be dangerously vicious, stopping things in midstream. I suspect it is unnecessary there.



Hi Campbell Ritchie,

I used System.exit(0) because correct execution of all build files is the prerequisite for further Program execution.
If there are any failures in build file execution then there is no point in continuing further execution.

Could you please suggest any other method to terminate the execution in more graceful way?

Thanks,
Ranjeet
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranjeet Deshmukh wrote:I used System.exit(0) because correct execution of all build files is the prerequisite for further Program execution.
If there are any failures in build file execution then there is no point in continuing further execution.


But System.exit(0) terminates the program, so it won't continue to execute - unless it's part of a script that then invokes something else.

Could you please suggest any other method to terminate the execution in more graceful way?


Well, the most obvious one to me would be to throw an Exception if an error DOES occur, and let it terminate naturally if it doesn't.
But it depends entirely on what your whole setup is, which you haven't described to us.

Winston

PS: Your code lines are far too long, which makes this thread difficult to read. Please read the link before you post any more code. Thanks.
 
Ranjeet Deshmukh
Ranch Hand
Posts: 35
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Winston,

I modified code to throw exceptions when any build file is failed and avoided use of System.exit. Also, from next time I will take care of far too long code lines.
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic