74

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?

1
  • 15
    The answer is different for each language.
    – Mark Byers
    Jul 18, 2012 at 22:42

10 Answers 10

58

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

3
  • 9
    I'm surprised this is the most voted for answer. At least for Java the answers from Code-Guru and @CosmicComputer seem more relevant for most people. I'd say very few developers, except the few shipping "executable" jars, ever use the manifest method. Jul 19, 2012 at 11:22
  • 3
    @GeorgeHawkins it seems to me this is the only answer that answers the question for both languages, although for Java it is perhaps not the best answer.
    – 11684
    Jul 25, 2012 at 8:53
  • Worked for me and clarified the difference b/w java and .net on this aspect. Thanks!
    – Anurag
    Feb 25 at 11:02
58

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.

5
  • 3
    In Java, it searches for public static void main(String[] args). Jul 18, 2012 at 22:48
  • 7
    @LuiggiMendoza Yes, that is correct. For brevity, I left out the full signature for main() since the OP already demonstrated he knows what it is. Jul 18, 2012 at 22:50
  • 5
    Or for a method signature public static void main(String[]) or public static void main(String ...) to be very precise. Jul 18, 2012 at 23:08
  • 2
    The other main methods aren't "ignored" they just aren't automatically executed. It is possible for a programmer to write code that explicitly invokes the main method of a class. Jul 24, 2012 at 17:55
  • 3
    Woohoo! My first silver badge! Thanks to everyone who voted for this answer! Jul 24, 2012 at 18:56
20

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.

4
  • 1
    I don't know about C#, but in Java, this is incorrect. The Java compiler does not care which entry point will be used; it will compile all of them. On the other hand, the run-time environment must be told which entry point to execute. Jul 18, 2012 at 22:52
  • 3
    yeah...but this ans was in reference to c# not java
    – Anirudha
    Jul 18, 2012 at 22:56
  • 3
    @Code-Guru: The answer is correct for C#, as an assembly has an entry point specified at compile-time - you don't specify a classname at execution time, just the assembly name.
    – Jon Skeet
    Jul 18, 2012 at 22:58
  • 2
    @JonSkeet The original answer did not specify the language. Thanks for the edit, Anirudha. Jul 18, 2012 at 23:15
12

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

10

In C#, you specify the entry point using the /main: compiler option.

Consider the following code containing two main() functions:

namespace Application {
    class ClassOne {
        static void main () {
            // Code here
        }
    }

    class ClassTwo {
        static void main () {
            // Code here
        }
    }
}

To use ClassOne.main() as your entry point, you would specify the following when compiling:

csc /main:Application.ClassOne hello.cs
8

For multiple main functions entry point can be declared by :

To set this compiler option in the Visual Studio development environment

Open the project's Properties page.

Click the Application property page.

Modify the Startup object property.

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

3
  • Do u have any idea to do the same thing in JetBrain Rider? bec I tried to look at project application properties and couldnt find Startup object
    – VectorX
    Sep 12, 2019 at 7:55
  • Sorry, don't have JetBrains installed on my machine.
    – NG.
    Sep 16, 2019 at 9:20
  • To change the startup object in Rider you'll have to edit the .csproj file (maybe with F4, depends on keybindings). Jul 21, 2020 at 10:34
7

In Java, as others pointed out, you define the class containing your main function when you run the java command.

But you could also build an executable jar, which can be run with java -jar my.jar. In this case, you need a manifest file called MANIFEST.MF in the folder META-INF in the jar. In this file, you specify the class containing the main function using the statement: Main-Class: YourClass.

4

The main method is static, which means it belongs to the class rather than the object. So the object won't have another main method inside it at all. It won't have an additional main-method, as main is static. So it's once per class.

If you have multiple main-methods in your project, you will specify which one to launch when starting your application

1
  • Just for clarification, a class can have more than one main method. For example, you could have public static void main() and public static void main(String[]). In Java, the run-time environment will look for the latter signature to run. Jul 19, 2012 at 14:30
2

In fact, in binary file, for example, PE format in windows and ELF format in Linux or any other system, The header of binary file will specify where is the start address and there can be only one.

Which one should be the entry point? It depends on the linker. Just like @SetFreeByTruth said that you can specify it with command line parameters. Many linkers support specifying entry point with command line parameters. for example, gnu's gld can specify entry point with parameter -e.

I don't know the behavior of Java because it is loaded by Java virtual machine.

-1

In Visual Studio, you select the project that you want to be the entry point, right click and Set as StartUp Project.

1
  • that would be the "startup project", but that's not what they mean. If you have multiple potential entry points in one project/assembly, you would open the project properties and choose from the "startup object" dropdown in the application pane. Dec 12, 2016 at 20:47

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.