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?

Ben
  • 1,233
  • 2
  • 14
  • 17

10 Answers10

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
57

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

MStodd
  • 4,716
  • 3
  • 30
  • 50
  • 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. – George Hawkins Jul 19 '12 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 '12 at 08:53
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.

Anirudha
  • 32,393
  • 7
  • 68
  • 89
  • 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. – Code-Apprentice Jul 18 '12 at 22:52
  • 3
    yeah...but this ans was in reference to c# not java – Anirudha Jul 18 '12 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 '12 at 22:58
  • 2
    @JonSkeet The original answer did not specify the language. Thanks for the edit, Anirudha. – Code-Apprentice Jul 18 '12 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

algorowara
  • 1,700
  • 1
  • 15
  • 15
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
SetFreeByTruth
  • 819
  • 8
  • 23
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

NG.
  • 5,695
  • 2
  • 19
  • 30
  • 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 '19 at 07:55
  • Sorry, don't have JetBrains installed on my machine. – NG. Sep 16 '19 at 09:20
  • To change the startup object in Rider you'll have to edit the .csproj file (maybe with F4, depends on keybindings). – Siro Mateos Jul 21 '20 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.

Sven Viehmeier
  • 418
  • 4
  • 11
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

Yasir Mahmood
  • 344
  • 1
  • 3
  • 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. – Code-Apprentice Jul 19 '12 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.

Johnny WU
  • 64
  • 2
-1

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

  • 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. – Cee McSharpface Dec 12 '16 at 20:47