How tor eturn exit code from Java to a shell script

M

Mark McFarlane

Since main is defined as

static void main()

How do I return a value to a shell script so I can check the 'tatus' of
the execution of a Java program from within the shell script?

Thanks,

Mark
 
E

Ed Kirwan

Mark said:
I meant to type 'status', not 'tatus'

You can return values via System.exit(). For example:

class Test {
public static void main(String [] args) {
System.exit(12);
}
}

In DOS, you'll see the result in ERRORLEVEL:
java Test
echo %ERRORLEVEL%
 
M

Mark McFarlane

Thanks Thomas and Ed,

Telling the JVM to shutdown by calling System.Exit() seemed a little
extreme to me (I don't know if there would be any undesirable side
effects of this call), but if it is the conventional practice and the
only option that's OK.

Mark
 
J

James McGill

It is simply a matter of exiting the program that you have written.

Well, it's true that it's the normal way to exit a single-function
"main" style program, but System.exit does more than just exit your
program -- it also shuts down the vm, takes down running threads, may or
may not run finally{}'s, etc. It is a pretty harsh way to exit a
program, more the equivalent of an OS shutdown than a function exit.

On the other hand, to do any better you have to do some form of IPC.
I realize the OP is doing something relatively trivial with a program
that runs in main() and so is nervous about using exit(). It's
perfectly reasonable and common to exit main with System.exit(), but
that doesn't stop it from being a nasty, ugly thing to do. It's also
the only realistic way to get a shell exit code for a utility program.
 
R

Roedy Green

static void main()

How do I return a value to a shell script so I can check the 'tatus' of
the execution of a Java program from within the shell script?
it is actually:

public static void main (String[] args)

and that all matters.

use the exit( int ) method.
 
Joined
Jun 22, 2010
Messages
1
Reaction score
0
Are you really just asking for "How do I return an error code from Java to the OS for use in a script?" If that's the case, then simply throwing a throwing an exception from the main method will result in the JVM returning a value of 1. This would be preferential to using System.exit(int) in such a case since any finally blocks will still get executed. On a Unix/Linux box, the shell variable $? will have a value of 1 if an exception is thrown by your main method and 0 for successful program completion.

Hope that helps.
 
Joined
Dec 20, 2011
Messages
1
Reaction score
0
simple usage of system.exit(0) which i have tried and its working abosolutly fine.

try{
System.out.println(" ** Java Program to Except Name and Age through Console ** ");
System.out.println(" ** Enter Your Name and Age **");
Scanner in = new Scanner(System.in);
name=in.nextLine();
age=in.nextInt();
in.close();
System.out.println("Your Name - "+name);
System.out.println("and your Age - "+age);
}
catch (Exception e)
{
System.out.println(" Error : "+e);
}
finally
{
System.exit(0);
}
}

source : http://javamazon.com/2011/12/20/how-to-read-input-from-console-keyboard-in-java/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top