Search code examples
c#javaprogram-entry-point

Multiple Main Functions


I'm a bit new at this so bear with me. I'm currently learning C# and Java and one of their similarities is that the main function needs to be encapsulated within a class. For example

public class HelloWorld {
    public static void main(String[] args) {
        // Some Code
    }
}

Now I understand that main is often the "entry point" when you run the program. So basically, your program will start executing wherever the main function is. But I believe in both languages you can have multiple main functions within multiple classes. So when I compile a project with multiple main functions, where is the "entry point"? How does the compiler know where to start?


Solution

  • 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