Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning error code from jar application

 
Makesh Kumar Ramakrishnan
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure, you came across the similar situation or there is a solution for this problem.

I need to return an error code from my jar application to the calling application. This jar application does some core java functionality and if it finds any unusal situation while executing the code, then it returns with the error code to the calling function. The calling function may be a unix shell script.

Can I use System.exit(n) to return the error code? Can this return value be received by the calling function? Please let me know your answers

Thanks,
Makesh
 
Chandra Sagi
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally System.exit(0) kills the JVM, so it may not return you any values.

Thanks
Chandu
 
Makesh Kumar Ramakrishnan
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is, System.exit(0) is a normal exit of JVM. and System.exit(1) is forced shutdown of JVM.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
API
[ February 27, 2006: Message edited by: Chetan Parekh ]
 
Makesh Kumar Ramakrishnan
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can this returned status code be properly received by the calling application?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Makesh Kumar Ramakrishnan:
Can this returned status code be properly received by the calling application?


Yes, it can. That is in fact the only purpose of the parameter - to serve as an error code returned to the caller.

It probably would have been faster and easier to simply try this, wouldn't it?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Makesh Kumar Ramakrishnan:
My understanding is, System.exit(0) is a normal exit of JVM. and System.exit(1) is forced shutdown of JVM.


No, the JVM exits in the same way, regardless of the error code. The code simply is passed to the operating system, which passes it to the caller (if the OS supports it).
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ilja Preuss, what System.exit(-1) do?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chetan Parekh:
Hey Ilja Preuss, what System.exit(-1) do?


The same as System.exit(42) or System.exit with any other value, just with a different exit code, of course.

Why do you ask? Did you expect it to do something special?
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


The same as System.exit(42) or System.exit with any other value, just with a different exit code, of course.

Why do you ask? Did you expect it to do something special?


Hey Ilja , you are right. I had a conception that when you use System.exit(-1), than only JVM will killed. I checked here and regardless of the values I pass, System.exit() kills the JVM.

Thnaks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic