Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Command-line arg

Thread view: 
joe - 19 Oct 2007 04:48 GMT
I was wondering if there is a way to check if there is a command line
argument (arg[0]) and not throw indexouofboundsException

Thanks
Lew - 19 Oct 2007 05:26 GMT
> 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 [i] );
     }
     break;
   }
 }
}

Signature

Lew

Wayne - 19 Oct 2007 05:27 GMT
> 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;
  }
Mark Space - 19 Oct 2007 20:19 GMT
>    ...
>    if ( arg.length > 0 ) {
[quoted text clipped - 7 lines]
>       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/
Wayne - 20 Oct 2007 00:34 GMT
> 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
Patricia Shanahan - 20 Oct 2007 00:46 GMT
>> 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:
...

> 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
Stefan Ram - 20 Oct 2007 01:01 GMT
>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
Joshua Cranmer - 20 Oct 2007 14:42 GMT
>> 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'

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Lew - 20 Oct 2007 14:51 GMT
Wayne <nospam@all4me.invalid> writes:
>>   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:

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.

>> 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'

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

Signature

Lew

Mike Schilling - 20 Oct 2007 01:04 GMT
> <quote>
> The method main must be declared public, static, and void. It must
[quoted text clipped - 18 lines]
> main as:
> public static void main ( String args[] )

I expect

   public static void main (final String[] args)

woiuld work too.
Lew - 20 Oct 2007 03:26 GMT
Wayne wrote:
>> <quote>
>> The method main must be declared public, static, and void. It must
[quoted text clipped - 18 lines]
>> main as:
>> public static void main ( String args[] )

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

Signature

Lew

Daniel Pitts - 20 Oct 2007 19:14 GMT
> > 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
[quoted text clipped - 34 lines]
>
> -Wayne

Who said it was the JVM who passed it in?
SomeClass.main(new String[] {null, "oops", "npe"});
SomeClass.main(null);
C. Rühl - 30 Oct 2007 14:01 GMT
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
C. Rühl - 31 Oct 2007 16:20 GMT
On 30 Okt., 14:01, C. R?hl <ch...@cruehl.com> wrote:
> 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
[quoted text clipped - 4 lines]
>
> /chris

*push*

i really need help with this. :(
Owen Jacobson - 31 Oct 2007 17:21 GMT
On Oct 31, 8:20 am, C. R?hl <ch...@cruehl.com> wrote:
> On 30 Okt., 14:01, C. R?hl <ch...@cruehl.com> wrote:
>
[quoted text clipped - 10 lines]
>
> 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.
Wayne - 31 Oct 2007 22:56 GMT
>> 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...

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
C. Rühl - 05 Nov 2007 10:26 GMT
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. :)
Roedy Green - 31 Oct 2007 21:47 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Andrew Thompson - 20 Oct 2007 14:48 GMT
...
>Thanks

Exactly!  I found this entire thread to be interesting,
from the original question, down through all replies.

Thanks to all contributors.  :-)

Signature

Andrew Thompson
http://www.athompson.info/andrew/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.