- Web servers like : Apache Tomcat.
- App servers like : Weblogic.
- EJB containers.
- Android java container. 5> all the other java implementations?

- 168,117
- 40
- 217
- 433

- 3,429
- 2
- 13
- 5
-
3All Java programs must start with a main method. Sometimes the method is visible to the programmer, and sometimes (such as with applets), it's not. – Hovercraft Full Of Eels Feb 14 '13 at 03:07
-
It depends what you mean by "Java programs". Applets for example starts with `init`. – Pshemo Feb 14 '13 at 03:14
-
@Pshemo: but that ignores the fact that there is a main method required somewhere, in this situation in the applet launching code, away from programmer's eyes, but regardless, the main *must* be there. – Hovercraft Full Of Eels Feb 14 '13 at 03:15
-
@HovercraftFullOfEels your right. To start JVM we ned to use some variant of `java SomeClass` command which searches for `main` method in that class. – Pshemo Feb 14 '13 at 03:19
6 Answers
Usually yes. But no, it is not required ... as eloquently pointed out by this answer ... https://stackoverflow.com/a/2897323/1481262
Also, while instrumenting the code of applications (using the java.lang.instrument
package), the execution starts with the premain
method before the main
is executed. - [1] [2].
[1] http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html
[2] http://dhruba.name/2010/02/07/creation-dynamic-loading-and-instrumentation-with-javaagents/
Yes. When you start a java program, you specify the class to use, and it's static main
method is invoked with the command line arguments.

- 115,893
- 19
- 128
- 203
-
What about class is implementing threads then main functions is not needed. Everything goes in run method – big Feb 14 '13 at 03:14
-
@ShivamTiwari: but what code gets *started* in the JVM? There must be a main to get things started. – Hovercraft Full Of Eels Feb 14 '13 at 03:14
Yes all java programs begin with main, though it's not necessary for each class to have it's individual main function.

- 1,599
- 13
- 14
Yes, all programs should start with main
, It is something like contract between JDK and JRE.

- 1,274
- 3
- 18
- 32
The JRE always starts from a main()
method. However, it is possible to start up a JRE and use it to run several independent programs. For example, a browser will often run all its applets in the same JRE.

- 16,188
- 39
- 30
Theoretically, you can write a custom launcher instead of java.exe, like here http://www.codeproject.com/Articles/17352/JVM-Launcher. Take a look at how they invoke main
:
//Find the class
jclass jcJclass = psJNIEnv->FindClass(mainClassName);
//Find the main method id
jmethodID jmMainMethod =
psJNIEnv-> GetStaticMethodID(jcJclass, "main", "([Ljava/lang/String;)V");
//Call the main method.
psJNIEnv->CallStaticVoidMethod(jcJclass, jmMainMethod, joApplicationArgs);
that is, it could be any method

- 133,369
- 30
- 199
- 275