How does java know what class to invoke the main() method of?

  • Context: Java 
  • Thread starter Thread starter Whovian
  • Start date Start date
  • Tags Tags
    Class Java Method
Click For Summary
SUMMARY

In Java, the entry point for execution is determined by the manifest file within a JAR (Java Archive) file. The manifest file specifies the main class to invoke the main() method. Unlike C++ and C#, where the main function can vary based on the compiler or context, Java relies on the 'Main-Class' attribute in the manifest to identify which class to execute. This ensures that when a JAR is run, the correct main() method is invoked as intended.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with JAR file structure
  • Knowledge of Java manifest files
  • Basic concepts of entry points in programming
NEXT STEPS
  • Study the Java JAR file specification
  • Learn how to create and edit a manifest file in Java
  • Explore Java's execution model and class loading
  • Investigate differences in entry points across programming languages like C++ and C#
USEFUL FOR

Java developers, software engineers, and anyone involved in packaging Java applications into JAR files will benefit from this discussion.

Whovian
Messages
651
Reaction score
3
In C++, pretty much any global main() function gets run. In C#, amy class's Main() method gets run, and having multiple Main() methods in multiple classes should give a compilation error(?). But if we have a .jar, when running it, how do we know which class's main() method the .jar will run?
 
Technology news on Phys.org
Hey Whovian.

Actually for C and C++, you have to provide an entrypoint in many cases for different compilers and the 'main' is not always assumed to be main (although it is set by default to do this).

As for your question in Java, a quick google search gave this:

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

Similar threads

  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
3
Views
4K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K