Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java. System.exit(int status). Value for exit status

I saw a big amount of questions about System.exit(int status) on stackoverflow.

For example:

Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java

difference between System.exit(0) and System.exit(-1)

But I do not see answer on my partial question:

Do we have any direct connection between exit status and System Error Codes for Microsoft products(Microsoft site)?

Sure, we do not have any direct connections, but: can we make this connection?

If we run our java program on Windows OS and our java program has System.exit(int status) statement, can we use System Error Codes list or no?

Is it good idea or not?

What do you think about it? Any other ideas?

Example:

  • Java program was run via command line on Wisdows OS;
  • Our java program catched exception like FileNotFoundException;
  • After this we made decision to close our program via System.exit(int state);
  • In this case can exit status be equal 2?

because(Microsoft site):

ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified. 
like image 522
user471011 Avatar asked Oct 13 '13 19:10

user471011


People also ask

What does system exit () do in Java?

System. exit() method calls the exit method in class Runtime. It exits the current program by terminating Java Virtual Machine.

What does exit status 1 mean in Java?

Exit Code 1 means that a container terminated, typically due to an application error or an invalid reference. An application error is a programming error in any code running within the container.

What is System exit () used for?

exit() method exits current program by terminating running Java virtual machine. This method takes a status code. A non-zero value of status code is generally used to indicate abnormal termination.


2 Answers

The exit status is a value of your choice. If you call your program and then react based on the exit value returned after it has finished execution, you are free to handle them as you wish.

There is only one general rule, which is that exit status 0 assumes that your program completed without error. Furthermore a negative exit status is usually assumed to be a termination because of an unexpected error, while a positive exit status is assumed to be a (more or less) graceful termination.

But again: if there is no batch file calling your code, no one will ever react on the exit status, and therefore you can return whatever you want, it will have no effect.

like image 196
TwoThe Avatar answered Sep 28 '22 00:09

TwoThe


The exit code is simply informational and can be based on your design. The Microsoft codes are important because MS software may try to recognize them, likewise on Linux there is a different set of predefined agreed-upon codes. It won't hurt to use one of the sets.

like image 21
nanofarad Avatar answered Sep 28 '22 02:09

nanofarad