Command-line arg

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
 
L

Lew

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

Sure.

public class HowManyArgs
{
public static void main( String [] args )
{
switch( args.length )
{
case 0:
System.out.println( "No args" );
break;

case 1:
System.out.println( "One arg: "+ args [0] );
break;

default:
System.out.println( args.length +" args: " );
for ( int i = 0; i < args.length; ++i )
{
System.out.println( args );
}
break;
}
}
}
 
W

Wayne

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

If your main method is declared:

public static void main ( String [] arg )
{
...
}

Then "arg" is an array (of Strings) object. All array
objects support a ".length" property (really a final int field)
that contains the number of elements in the array. So you can
write code similar to the following to avoid that exception:

...
if ( arg.length > 0 ) {
... // do something with arg[0]
}

or:
...
if ( arg.length == 0 ) {
// do error handling here
return;
}
 
M

Mark Space

Wayne said:
...
if ( arg.length > 0 ) {
... // do something with arg[0]
}

or:
...
if ( arg.length == 0 ) {
// do error handling here
return;
}


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.

I was just at the Apache Commons project yesterday thanks to a pointer
from someone here on this newsgroup. Check out CLI, a generic command
line parser:

http://commons.apache.org/cli/
 
W

Wayne

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
 
P

Patricia Shanahan

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>

That makes a good case for assuming that main is invoked with a non-null
argument when it is the main method of an application. However, a Java
main method is a perfectly normal static method, and could be invoked by
other code.

That said, I think a main method can be assumed to expect a non-null
parameter, and a NullPointerException is a reasonable reaction to:

SomeClass.main(null);

Patricia
 
S

Stefan Ram

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«:

public class Main{ public static void main( final String[] args ){} }
class String{}

gives:

Exception in thread "main" java.lang.NoSuchMethodError: main
 
M

Mike Schilling

Wayne wrote:
:
<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[] )

I expect

public static void main (final String[] args)

woiuld work too.
 
L

Lew

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[] )

None of what you cite precludes passing null to main(), as Patricia pointed out.
 
J

Joshua Cranmer

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«:

The JLS mentions this in §1.2 Notation:
Throughout this book we refer to classes and interfaces drawn from the
Java and Java 2 platforms. Whenever we refer to a class or interface
which is not defined in an example in this book using a single
identifier N, the intended reference is to the class or interface named
N in the package java.lang. We use the canonical name (§6.7) for classes
or interfaces from packages other than java.lang.

Thus the class `String' implicitly refers to `java.lang.String'
 
L

Lew

Joshua said:
The JLS mentions this in §1.2 Notation:

So not only is Wayne right, but Sun agreed and made sure that the technical
specification was precise, even before Wayne suggested that they do so. How
very proactive of them.
Thus the class `String' implicitly refers to `java.lang.String'

This might be a mere coincidence, but in Java code one need never explicitly
import java.lang either.
 
D

Daniel Pitts

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

Who said it was the JVM who passed it in?
SomeClass.main(new String[] {null, "oops", "npe"});
SomeClass.main(null);
 
?

=?iso-8859-1?B?Qy4gUvxobA==?=

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
 
?

=?iso-8859-1?B?Qy4gUvxobA==?=

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. :(
 
O

Owen Jacobson

*push*

i really need help with this. :(

The only place the java runtime provides command-line arguments to
programs is as the arguments to main(String...). To get them anywhere
else, you have to pass the argument or arguments you're interested in
to where they need to go.

If the OSGi runtime provides its own main, there may be a way to get
the arguments through the OSGi API. Check the docs. Otherwise, add
an interface to your bundle for passing in parameter lists and pass
the args you care about in.
 
R

Roedy Green

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.

main is also an ordinary method. This code is legit

SomeClass.main( null );

So if that is possible, you need to check for null too. It would be
nice to have Eiffelian logic to simply declare null is not a legit
parameter.
 
W

Wayne

I don't think you can. I didn't know anything about OSGI bundles
so I looked it up. It seems this is a framework or application
server ("container") that uses a single JVM instance to run multiple
applications, packaged as "bundles" which are jar files with extra
manifest entries.

Now command line arguments are passed to main from the OS when the
JVM starts (and runs main). Thus, you don't launch a bundle with
any command line arguments, since you deploy a bundle on an already
running JVM.

What you can do is to include a properties file (or use the OSGI prefs
API) to include some data that can be read from the bundle at
runtime. You are allowed to include such resource files (even a simple
text file with the command line args) in OSGI bundles.

<nostalgia-mode>
I remember C development on a Mac solved a similar problem by showing
a dialog box at runtime, so a user could enter in command line arguments.
This was before OS X of course.
</nostalgia-mode>

OSGI sounds interesting. Is this technology popular?

-Wayne
 
?

=?iso-8859-1?B?Qy4gUvxobA==?=

i solved that problem giving a VM argument >>-Dxmlfile="D:/.../.../
temp.xml"<< and getting it using System.getProperty("xmlFile") in one
of my classes. working fine!

thank you for all your help! :)

to wayne: i think OSGi is popular indeed, but it's not a boom-thing.
means: not everybody uses it yet because it seems to be very
complicated right after you start experimenting with it. but i think
once you foud out how to work with it efficently, its a self-go. :)
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top