Some basic features

I'm a little new to this, so bear with me. I am currently studying C # and Java, and one of their similarities is that the main function must be encapsulated inside the class. for example

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

Now I understand that the main part is often the "entry point" when the program starts. Thus, your program will start execution, wherever the main function is. But I believe that in both languages ​​you can have several basic functions in several classes. So, when I compile a project with several basic functions, where is the entry point? How does the compiler know where to start?

+67
java c # main
Jul 18 2018-12-18T00:
source share
10 answers

In .NET, you can determine which class contains the Main method that you want to use when compiling.

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

In Java, if you get attached to a jar, you can define your entry point in the jar manifest.

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

+54
Jul 18 2018-12-18T00:
source share

In Java, a computer defines an β€œentry point” when you are actually executing a program, not when you are compiling. For example, from the command line

 java MyClass 

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

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

+57
Jul 18 2018-12-18T00:
source share

In C #, you can use several Main methods.

If there are several Main methods, the compiler does not know which entry point to use, and therefore it will show you an error.

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

+15
Jul 18 2018-12-18T00:
source share

The main class is a special class for only one reason: when starting the Java virtual machine, this function is what calls the JVM. This is essentially just like any other function, and in fact you can call one main class function from another class.

When compiling a project with multiple classes, you tell the JVM to start the class with the main class that you want to use, for example:

 java SomeClass 

and it will run the SomeClass main method, assuming that SomeClass is compiled and that the corresponding compiled file is in your class path. (This is what you will need to solve with your specific OS, but I believe that for the -cp option it is enough to specify the class path). So:

 java -cp /home/MyName Someclass 

the main function SomeClass will be launched in the directory / home / MyName

+12
Jul 18 2018-12-18T00:
source share

In C #, you specify an 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 an entry point, you must specify the following when compiling:

 csc /main:Application.ClassOne hello.cs 
+9
Jul 18 2018-12-18T00:
source share

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

But you can also create an executable jar that can be launched using java -jar my.jar . In this case, you will need a manifest file named MANIFEST.MF in the META-INF folder in the bank. In this file, you specify the class containing the main function using the instruction: Main-Class: YourClass .

+7
Jul 18 '12 at 23:11
source share

For several basic functions, an entry point can be declared:

To set this compiler option in the Visual Studio development environment

Open the project properties page.

Go to the application properties page.

Change the property of the launch object.

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

+7
Jul 19 '12 at 2:26
source share

The main method is static, which means that it belongs to a class, not an object. Thus, the object will not have another main method inside it. It will not have an additional main method, since the main one is static. So this is once in class.

If you have several basic methods in your project, you will indicate which one to start when the application starts.

+4
Jul 19 '12 at 5:18
source share

In fact, in a binary file, for example, in PE format on Windows and in ELF format on Linux or any other system, the header of the binary file will indicate where the starting address is, and there can only be one.

What should be the entry point? It depends on the linker. Like @SetFreeByTruth, you can specify it with command line options. Many linkers support specifying an entry point with command line parameters. for example, gnu gld can specify an entry point with the -e option.

I do not know the behavior of Java, because it is loaded by the Java virtual machine.

+2
Jul 25 2018-12-12T00:
source share

In Visual Studio, you select the project that you want to become an entry point for, right-click and select "Make As StartUp Project".

-one
Dec 12 '16 at 20:40
source share



All Articles