Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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?

share|improve this question

2 Answers 2

up vote 14 down vote accepted

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 which invoked the Test class's Main method rather than the one defined in the one defined in the Manifest.

share|improve this answer
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 Sep 12 '13 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). – MichaelT Sep 12 '13 at 19:28

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.

share|improve this answer
how does this answer the question asked? – gnat Sep 11 '13 at 7:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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