Reputation: 495
I am currently looking over some java code not written by me. In the java project there are mutiple packages, and I see three main methods, and they are all in different classes. Two of the classes containing the main method are in the same package, and the other main is in a seperate package. The main method alone in the seperate package seems to be called when I execute the code.
The first main method is
public static void main(String[] args) {
//code written in here seperate package
}
Second main method is
public static void main(String[] args) {
//code written in here same package as third main
}
Third main method is
public static void main(String[] argv) {
//code written in here same package as second main
}
All three method contain the same parameter, so it shouldn't be overloaded. They all have code that performs different functionality. How is this possible? How does java know which method to call? I am not getting any errors when executing this code through java jar command in the command line.
Upvotes: 1
Views: 227
Reputation: 4784
Edited
main
in Java, you have to give the class name also. So this way there is no ambiguity. In your case, unzip your jar file with any unzip program, and look for a file called MANIFEST.MF. This file will contain the name of the main class.main
method, use an alternate command-line, specifting the class that contains the main
method you want to use something similar to:
java -classpath somejar.jar com.example.Foo
Sualeh.
Upvotes: 2
Reputation: 719
You have many classes with main method. So, you can run these many classes. To run particular class you have to mention it implicitly:
java -classpath . Foo
where Foo is class you want to use to run main method. When you run java without class name then default class is used to run main method. Default main class is usually defined in the manifest.
Upvotes: 0
Reputation: 1859
you can define main class of jar at jar creation time and there you have to set only one class name so no scope of ambiguity and second way is run jar file with command
java -jar jarfilename.jar foo('foo' is class name that has main method)
so there is no scope of ambiguity
Upvotes: 0
Reputation: 4288
Calling a static method requires a class name during the invocation. Calling the main method is no different. Therefore, it doesn't make a difference that the method in question is the "main" method. There are no restrictions in Java about static methods being named uniquely (or overloaded) if they are in different classes.
Upvotes: 0
Reputation: 140427
Java doesn't know which method to call.
That is something that the user specifies when starting the JVM. At that point, you select a "target class"; and the JVM will search for a main method in that class. In other words: when you invoke "java" on the command line, you tell which main()
method should be executed - by selecting a class name!
It could be that your application is deployed as a JAR archive; and the manifest file within that JAR file can point out that "target" class, too.
Upvotes: 1