Multiple Main Functions

In Java, the computer determines the "entry point" when you actually execute the program, not when you compile. For example, from the command-line

java MyClass

searches for main() in MyClass. All other main() functions are ignored.

If you are using an IDE, then you can set which class contains the main() function that you want to use.


In .NET, you can define which class contains the Main method you want to use when you're compiling.

http://msdn.microsoft.com/en-us/library/x3eht538.aspx

In Java, if you're bundling to a jar, you can define your entry point in the jar's manifest.

http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html


In C#, you can use multiple Main methods.

If there are multiple Main methods, the compiler doesn't know which entry point to use and hence it will show you an error.

You need to specify the Main method to be used at compilation: You can specify which method to be used as a compiler option in the Visual Studio development environment or through the csc compiler.


The main class is a special class for only one reason: when you run the Java Virtual Machine, that function is what the JVM calls. It is essentially like any other function, and in fact you can call one class's main function from another class.

When you compile a project with multiple classes, you tell the JVM to run the class with the main class you want to use, like so:

java SomeClass

and it will run the main method of SomeClass, assuming that SomeClass is compiled and that the appropriate compiled file is in your classpath. (That is something you'll have to work out with your particular OS, but I think it's fairly usual for the -cp option to specify a classpath). So this:

java -cp /home/MyName Someclass

will run the main function of SomeClass in the directory /home/MyName