In C and C++, the main method holds the filename in the first position of the array at argv[0]. In Java, however, the filename is not included in the args string array.

Is there a practical reason for this? I understand that this makes iterating through command line arguments 0-based instead of 1-based, but is there a benefit? Was the filename just deemed to be useless?

2 Answers 2


In some cases a program can be run in different ways and exhibit different behavior on how it is called. If you call vim as vi, it runs in a compatibility mode. Sometimes it is to try to maintain one version of several related programs - for example mailq and newaliases on many unix systems are a link to sendmail so that these programs stay in sync)

Java programs are typically invoked as:

% java -jar foo.jar args
% java Foo args

The first version is where you have a Manifest file that indicates the main class, the second version runs the main method in the class Foo found in the class path.

The information presented for Java is either a path to the jar or the name of the class being invoked.

The location of the jar isn't important enough to be something to code from (and was actually not part of the original spec). A Jar can be named anything really, and often includes version numbers. Whats more, there's no guarantee that the class was even stored in a .jar (it could have been extracted).

Invoking a Java application with -jar has only one way to enter it - the class defined in the Manifest. There's no renaming that can be done.

The other option, of invoking it with the class name points directly to the execution unit. Furthemore, it can't be named multiply - you can't have Bar.class be the code for class Foo it just doesn't work that way.

This should show that there's really no point to passing the information of argv[0] in the C sense to a Java application - its either going to be java, meaningless and arbitrary, or the name of the class that is being invoked (that you are already executing code out of (you could do something like getClass().getEnclosingClass().getName() if you were desperate...)).

There is a point here, you can define multiple Main methods in classes in a .jar or on the class path. And you could have them behave differently just as if there was a series of if statements based on what argv[0] was.

I have in the past had code akin to java -cp Foo.jar com.me.foo.Test which invoked the Test class's Main method rather than the one defined in the one defined in the Manifest.

  • There has to be more to it than that. In C#, the parameters don't contain the file name, but the application is usually executed directly, just foo.exe.
    – svick
    Commented Sep 12, 2013 at 19:14
  • @svick I'm not familiar with C#, nor how an exe is packaged. In a few OS's you can make a jar executable (see this) which launches the entry point defined in the Manifest. Similar things may be done for C#. The key things are you can't change the entry point by changing the name of the file, and the file name isn't intended to be used by any other parts of the application (outside the class loader).
    – user40980
    Commented Sep 12, 2013 at 19:28
  • @nqzero (context) - If I specify java com.me.Foo as the command line, the method com.me.Foo.main(String...) is being invoked. There is no way around that. And I know that it is Foo that is being invoked - there's no reason to stick that in argv. It would be purely redundant information. Sure, it could be in the superclass, but I have the trivial opportunity to intercept it with the desired information of what the command line invocation was - no need to put it in argv.
    – user40980
    Commented Jan 19, 2016 at 20:57
  • ... and please remember to get 50 rep and comment rather than suggesting edits to the answer. It is a very poor way to raise issues with a given post.
    – user40980
    Commented Jan 19, 2016 at 20:58
  • Sometimes the behaviour is radically different. wput for instance is actually wget.
    – mckenzm
    Commented Oct 2, 2019 at 6:16

actually there's no benefit with it, it really depends on the syntax of the programming language you are using if it is 0-based or 1-based. the variable (you refer to as filename) also depends on the language, it can be different in other languages just follow the correct syntax of the language you are using.

  • 1
    how does this answer the question asked?
    – gnat
    Commented Sep 11, 2013 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.