J
joe
I was wondering if there is a way to check if there is a command line
argument (arg[0]) and not throw indexouofboundsException
Thanks
argument (arg[0]) and not throw indexouofboundsException
Thanks
joe said:I was wondering if there is a way to check if there is a command line
argument (arg[0]) and not throw indexouofboundsException
joe said:I was wondering if there is a way to check if there is a command line
argument (arg[0]) and not throw indexouofboundsException
Thanks
Wayne said:...
if ( arg.length > 0 ) {
... // do something with arg[0]
}
or:
...
if ( arg.length == 0 ) {
// do error handling here
return;
}
Mark said:It is theoretically possible for main() to be called with a null
argument. You may wish to check for this also or risk throwing a
NullPointer exception.
Wayne said:Nope. Section 12.1.4 of the JLS: ....
And Section 2.16.1 of the JVM spec:
<quote>
A Java Virtual Machine starts execution by invoking the
method main of some specified class, passing it a single
argument, which is an array of Strings. ...
</quote>
Wayne said:public static void main(String[] args)
<quote>
The method main must be declared public, static, and void. It must
accept a single argument that is an array of strings. This method
can be declared as either
public static void main(String[] args)
or
public static void main(String... args)
</quote>
And Section 2.16.1 of the JVM spec:
<quote>
A Java Virtual Machine starts execution by invoking the
method main of some specified class, passing it a single
argument, which is an array of Strings. ...
</quote>
I think the only legal but not stated variation is declaring
main as:
public static void main ( String args[] )
Wayne said:<quote>
The method main must be declared public, static, and void. It must
accept a single argument that is an array of strings. This method
can be declared as either
public static void main(String[] args)
or
public static void main(String... args)
</quote>
And Section 2.16.1 of the JVM spec:
<quote>
A Java Virtual Machine starts execution by invoking the
method main of some specified class, passing it a single
argument, which is an array of Strings. ...
</quote>
I think the only legal but not stated variation is declaring
main as:
public static void main ( String args[] )
Stefan said:Wayne said:public static void main(String[] args)
Technical specifications could be more precise and either
use a specific import declaration or »java.lang.String«:
Thanks
Joshua said:The JLS mentions this in §1.2 Notation:
Thus the class `String' implicitly refers to `java.lang.String'
Mark said:It is theoretically possible for main() to be called with a null
argument. You may wish to check for this also or risk throwing a
NullPointer exception.
Nope. Section 12.1.4 of the JLS:
<quote>
The method main must be declared public, static, and void. It must
accept a single argument that is an array of strings. This method
can be declared as either
public static void main(String[] args)
or
public static void main(String... args)
</quote>
And Section 2.16.1 of the JVM spec:
<quote>
A Java Virtual Machine starts execution by invoking the
method main of some specified class, passing it a single
argument, which is an array of Strings. ...
</quote>
I think the only legal but not stated variation is declaring
main as:
public static void main ( String args[] )
So, if your JVM is capable of passing a null to main,
it is not a legal JVM. Invoking args.length in main
can not throw a NullPointerException in any valid JRE.
Have you ever run across a JVM that passed a NULL to main?
I've never tested any JVMs for that, but if there are
popular JVMs that do this I guess such a test would be
worthwhile.
-Wayne
how would you get command line arguments in an osgi-bundle? i'm facing
that problem right now. i have a number of bundles and i want to start
one (or lets say the manager-bundle) with a xml-file that should be
parsed...
thank you!
/chris
*push*
i really need help with this.![]()
So, if your JVM is capable of passing a null to main,
it is not a legal JVM. Invoking args.length in main
can not throw a NullPointerException in any valid JRE.
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.