Why is the main() of Java void ?

K

Kislay

In c/c++ the main() function returns a value to indicate whether its
termination was normal or abnormal . ? Is there no such need in
Java ? If there is , then why is the main() method void ? How would
the OS know that exit was normal or abnormal ?
 
T

Thomas Kellerer

Kislay, 15.10.2007 08:50:
In c/c++ the main() function returns a value to indicate whether its
termination was normal or abnormal . ? Is there no such need in
Java ? If there is , then why is the main() method void ? How would
the OS know that exit was normal or abnormal ?
In java you use System.exit(int) to return a status value to the OS
 
A

Andrew Thompson

K

Kislay

But does this value actually reach the OS ? Java is platform
independent , it runs on JVM . Does the value returned by
System.exit(int) actually reach the OS . Also , does it need to ?
Wouldn't its interaction with the OS would kind of against its
platform-independent nature ?
 
C

Christian

Kislay said:
But does this value actually reach the OS ? Java is platform
independent , it runs on JVM . Does the value returned by
System.exit(int) actually reach the OS . Also , does it need to ?
Wouldn't its interaction with the OS would kind of against its
platform-independent nature ?

we need to interact with the os in a lot of ways..

java is platform independent means that os calls have a java layer
wrapped around them.
So for reading from a file you need the os.. though the jvm will work as
a layer between you and the os and call the appropriate native
functionality so you can write your code platform independent.
 
T

Thomas Kellerer

Kislay, 15.10.2007 14:10:
But does this value actually reach the OS ?
Why don't you simply try it.
Also , does it need to ?
Depends on your requirements.
Wouldn't its interaction with the OS would kind of against its
platform-independent nature ?

Java uses features that are common across all OS. And passing a return
value back to the OS is something that every OS supports (even DOS
supported that)


Thomas

P.S.: when typing, you usually do not put a space in front of
punctuation marks, only after them.
 
K

Kislay

Thanks Mr. Kellerer. And I'll work on that punctuation thingy :). Can
you give me an idea as to how can I test whether the value returned by
System.exit(int) reaches the OS?
 
F

Flo 'Irian' Schaetz

And thus spoke Kislay...
Thanks Mr. Kellerer. And I'll work on that punctuation thingy :). Can
you give me an idea as to how can I test whether the value returned by
System.exit(int) reaches the OS?

Depends on your OS :) But in Windows you could simply write a simple
Batch-File...

@echo off
echo.%ERRORLEVEL%
pause

....which will show you the last exit code (should work on Win2000 and
newer, don't know about Vista.

Then simply run your Java program and when it's ready, start the batch file.

Flo
 
T

Thomas Kellerer

Flo 'Irian' Schaetz, 15.10.2007 15:24:
And thus spoke Kislay...


Depends on your OS :) But in Windows you could simply write a simple
Batch-File...

@echo off
echo.%ERRORLEVEL%
pause

...which will show you the last exit code (should work on Win2000 and
newer, don't know about Vista.

Then simply run your Java program and when it's ready, start the batch file.

Not sure if that would work (starting the batch file after running the
Java program), but if you put both things into one batch file it will work:


@echo off
java -cp myapp.jar MyMainClass
echo %ERRORLEVEL%


Thomas
 
L

Lew

Kislay said:
Thanks Mr. Kellerer. And I'll work on that punctuation thingy :). Can
you give me an idea as to how can I test whether the value returned by
System.exit(int) reaches the OS?

Given the class:
<sscce>
package testit;
public class ExitTester
{
public static void main( String [] args )
{
return (args.length > 0? System.exit( 0 ) : System.exit( 1 ));
}
}
</sscce>

compile it and run the following commands (assumes bash; adapt for Windows
shell if you're running Windows shell):

$ java testit.ExitTester
$ echo $?
$ java testit.ExitTester argument
$ echo $?

Kislay:
Java is platform independent , it runs on JVM .
java [sic] is platform independent means that os calls have a java layer
wrapped around them.

Java is not completely platform independent; it requires a JVM. Java is
platform independent in the way that Christian said - it must have a JVM to
handle OS-specific actions, like relaying the result of System.exit() to the OS.

The JVM is not at all platform independent.
 
F

Flo 'Irian' Schaetz

And thus spoke Thomas Kellerer...
Not sure if that would work (starting the batch file after running the
Java program)

It does, I tried it (simply Java with System.exit(12) and then starting
the .bat). It seems that Windows stores the ErrorLevel in the
environment variable %ERRORLEVEL% - so it stays there until changed.
 
L

Lew

Flo said:
And thus spoke Thomas Kellerer...


It does, I tried it (simply Java with System.exit(12) and then starting
the .bat). It seems that Windows stores the ErrorLevel in the
environment variable %ERRORLEVEL% - so it stays there until changed.

Then it would work from the command line interactively, not from a .bat, correct?
 
F

Flo 'Irian' Schaetz

And thus spoke Lew...
Then it would work from the command line interactively, not from a .bat, correct?

Yes, simply typing...

echo.%ERRORLEVEL%

....should also work...

Flo
 
L

Lew

Roedy said:
You use System.exit( n ) to pass an abnormal termination.

And it won't be the OS that "knows", it'll be something one codes to handle
the exit code.

By convention, zero represents normal termination.
 
C

Christopher Benson-Manica

[comp.lang.java.programmer] Thomas Kellerer said:
In java you use System.exit(int) to return a status value to the OS

C89 says that the status is unspecified if the return from main() is
omitted; C99 implicitly returns a status indicating "successful".
What status does a Java program return to the host environment at the
end of main()? Is it specified?
 
B

Bent C Dalager

[comp.lang.java.programmer] Thomas Kellerer said:
In java you use System.exit(int) to return a status value to the OS

C89 says that the status is unspecified if the return from main() is
omitted; C99 implicitly returns a status indicating "successful".
What status does a Java program return to the host environment at the
end of main()? Is it specified?

Java applications don't return anything at the end of main() - Java
applications return something when the application terminates. This
may be weeks after main() ended.

Unless forced to do otherwise, a Java application terminates when its
last non-daemon thread ends. If that thread ends normally (i.e. it
doesn't explicitly call System.exit(), Runtime.exit() or
Runtime.halt() but just runs to completion) then the app will
presumably return the "ok" value for the OS it runs on (normally 0).

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runtime.html#exit(int)

Cheers,
Bent D
 
R

Roedy Green

And it won't be the OS that "knows", it'll be something one codes to handle
the exit code.

It conceivable that there exist OSes that don't have a notion of exit
codes. Perhaps they have a boolean, or some other scheme. System.exit
can then translate

As a programmer it is a lot easier to die by calling System.exit than
arranging to pass the offending numeric code all the way back up the
stack to main.
 
R

Roedy Green

C89 says that the status is unspecified if the return from main() is
omitted; C99 implicitly returns a status indicating "successful".
What status does a Java program return to the host environment at the
end of main()? Is it specified?

Oddly, it behaves sanely in Windows. Without a System.exit or with
System.exit(0) you get a 0 return/ERRORLEVEL code
 
T

Thomas Kellerer

Christopher Benson-Manica, 15.10.2007 21:05:
[comp.lang.java.programmer] Thomas Kellerer said:
In java you use System.exit(int) to return a status value to the OS

C89 says that the status is unspecified if the return from main() is
omitted; C99 implicitly returns a status indicating "successful".
What status does a Java program return to the host environment at the
end of main()? Is it specified?
What is C89 and C99?
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top