If I kill an Android app with System.exit(...), does it matter what status code I pass? I couldn't find any documentation on whether Android just ignores it or whether certain ones lead to any error messages for example or have any other meaning.
This is the exit code returned by the process when it finishes; Android however does not care, but know that the error code should never be higher then 255.
Here is a list of standard exit codes - some process may use their own codes.
0 Clean Exit
1 General Error Catchall
2 Misuse of shell builtins
126 Command invoked execution error
127 Command not found
128 Invalid exit argument?
130 Termination by Control-C
255 Exit status unknown
Android does not care, no. This is a java thing -- you can use them for your own debugging etc etc.
Here's an example of system exit codes: http://www.opensource.apple.com/source/Libc/Libc-320/include/sysexits.h
But pretty much, anything > 0 means that its an abnormal exit. You can set it to whatever you want, but I'd just stick to 0 unless you're interacting with other things.
It just seems to be the accepted convention.
Related
I have an application which simulate Agile scrum board,developed in java which takes commands through command line.For e.g. it takes command like:
"create user story"
One of the rules to develop this application says that:
Your implementation should return a 0 errno on successful completion, 1 for an error
what does this errno means? is errno some specific keyword ?And if its keyword,then how to return this?
I have used system.exit() in below manner. Is it correct way of using it ? I dont get 0 printed on console when the program is exited?
if(input.equalsIgnoreCase("quit")){
allbacklogs.saveAllUserStory();
System.exit(0);
}
Thank
errno just means error number.
0 means that program excecution went fine
1 (Or any other integer than 0) means that there was an error
When the program ends naturally, 0 will be returned, and when the program crashes for uncatched exception or error, 1 will be returned automatically.
So you don't actually need to do anything to fit that requirement. If you need to manually use System.exit use System.exit(0) on no error and System.exit(1) on error.
Below is piece of program I was using to simply Open and close the Internet Explorer from my command line program. im running my program with Java 6 on Windows XP OS:
Runtime runtime = Runtime.getRuntime();
Process p1 = runtime.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe");
Thread.sleep(5000);
p1.destroy();
Thread.sleep(2000);
System.out.println("p1.exitValue(): "+p1.exitValue())
The exit value is : 1.
Javadoc says: by convention, the value 0 indicates normal termination.
http://download.oracle.com/javase/6/docs/api/java/lang/Process.html#exitValue()
Then I commented p1.destroy and instead of closing the browser from my Java program, I closed the window manually (File>Exit). In this case p1.exitValue started returning '0'.
My question is:
Why program returns exit code as '1' in first case? Does JVM treats p1.destroy() as abnormal way of terminating a program?
In general the 'exit status code' value is JVM specific or Operating system specific? I have seen some question where people have reported exit code value as '10', '34545' etc..
Thank you for reading,
Actually, that's two questions :-)
Almost certainly, IE itself captured the fact that it was being shut down externally and decided to return that error code (see 2 below). So no, the JVM doesn't treat p1.destroy() as a special case, but the affected process may.
Exit values are process specific, not JVM specific (and not even OS specific). In other words, a process itself returns a value to be used as the exit value. This makes sense when you think of the fact that there are ways to destroy processes which don't involve the JVM at all.
I should mention that there are cases where the process doesn't affect the exit code. Under some UNIX-like operating systems, if a process exits due to some serious fault (like a segmentation violation or a violent external shutdown), the exit code may be set by the OS to a value indicating this. From memory, it was something like 128 plus the signal number.
I'm running a Java program through a Python script on Linux, but the program crashes without outputting any error messages. The os.system command that executes the Java program outputs an error code of 24. What does this mean?
On my system this is found in /usr/include/asm-generic/errno-base.h:
#define EMFILE 24 /* Too many open files */
This means your process has exceeded the limit on C/system file descriptors. Generally the limit is around 1024, there may be a bug in that some file descriptors are not being closed. (This would seem unlikely in Python or Java code where it's done for you...).
Update0
I've just realised you may be talking about the return code from the Java program. This is program specific, you'll need to check the documentation or code for the program.
From http://docs.python.org/library/os.html#os.system:
On Unix, the return value is the exit
status of the process encoded in the
format specified for wait(). Note that
POSIX does not specify the meaning of
the return value of the C system()
function, so the return value of the
Python function is system-dependent.
Luckily, the C macros used to dissect the return status are available in the os module (starting here)
I ran these on amd64/Linux:
>>> import os
>>> os.WIFEXITED(24) #Return True if the process exited using the exit(2) system call
False
>>> os.WIFSIGNALED(24) #Return True if the process exited due to a signal
True
>>> os.WTERMSIG(24) #Return the signal which caused the process to exit
24
According to http://linux.die.net/man/7/signal, I think signal 24 may be SIGTSTP, (someone stopped the process by hitting CTRL+Z). What platform/architecture are you running on?
(Going forward, I would recommend using the subprocess module so that you can capture stdout/stderror)
update
Someone had posted it and then it disappeared, but signal 24 is likely SIGXCPU (CPU time limit exceeded)
If I have written a program in Java, how can I know if my program has terminated normally or exited normally?
I'm guessing a little here, since your question is kinda vague. But I assume you talk about finding out within your program that this program terminated normally or not when it was last run.
If you got something of a configuration directory or file—leave a note there, when the program starts and remove said note when your process exits normally. If that note is still there when your program starts then either another instance is still running (something you'd need to check separately then) or it didn't terminate normally.
The JVM returns an integer exit code upon termination of your program, with 0 meaning the program terminated correctly, and a non-zero value usually representing an error condition.
If your program comes to an end normally (i.e. the main method returns without throwing an exception), then the JVM will return 0, otherwise, if an exception is thrown and not caught, then the JVM will return a non-zero error code.
You can also set the error code yourself, via the System.exit(int) method. This causes the JVM to immediately terminate and return the specified error code.
Assuming you're on a Windows based machine, you can then see the error code returned by echoing the %ERRORLEVEL% property.
For example assuming you have a class Main, you can see the error code like this:
C:\>java Main
C:\>echo %ERRORLEVEL%
0
If you then changed main to call System.exit(1), then the error level would look like this:
C:\>echo %ERRORLEVEL%
1
I have a C# app that runs a jar file and uses the System.exit code as a return value. A console window is displayed as a part of this jar, and if the user closes it (thereby terminating the jar) it returns an exit code of 143. The problem is that 143 (or any positive integer) could be a valid exit code if the jar completes successfully.
I need a way to manually set the System.exit code to a negative integer. Treating 143 as an exception in the C# app is out of the question.
As in Diego Dias answer,
The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.
If you really want to ignore that, and both the Java and the C# apps are in your hand, an easy workaround is to add 1000 to your System.exit return value in Java, when the jar completes.
Your C# application will recognize the successful execution by a return code >= 1000 and subtract it again. 143 is below 1000 and thus an error.
Acording to the java documentation you can use System.exit(int n), so just put a negative number as a parameter, also by convention, a nonzero status code indicates abnormal termination.