Can we execute a java program without a main() method?
Now - no
Prior to Java 7:
Yes, sequence is as follows:
- jvm loads class
- executes static blocks
- looks for main method and invokes it
So, if there's code in a static block, it will be executed. But there's no point in doing that.
How to test that:
public final class Test {
static {
System.out.println("FOO");
}
}
Then if you try to run the class (either form command line with java Test
or with an IDE), the result is:
FOO
java.lang.NoSuchMethodError: main
Run a Java program without main method?
When you compile run the code, the compiler searchs for main method, that means without main method you can't go to runtime because of the creation of the objects, maybe your code compiles while compiling time, but that's something else.
So, it's not possible to run an aplication without main method.
How do Java programs run without defining the main method?
The main
method is only used when the Java Virtual Machine is executing your code. Code cannot be executed without a main
method but it can still be compiled.
When compiling code, you usually specify a set of files on the command line e.g.
javac MyClass1.java MyClass2.java
The Java compiler (javac
) examines each class you passed to it and compiles it into a .class file.
One reason Java source code may be missing a main
method is because it is designed to be used as a library, instead of being executed.
Something you may find interesting: although the source code compiled by the Java compiler does not need a main
method, the source code for the Java compiler itself does have a main
method.
How can you run a Java program without main method?
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello
and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0)
immediately after printing the message.
As of JDK6 onward, you no longer see the message from the static
initializer block; details here.
Java class without main method will run?
Q1. can we compile this java class? Note we don't have a main method in this class.
Yes, that class should compile. There's no requirement that says you need a main
method in every class for it to compile. (Most of your classes won't have their own main
method.)
Q2. Is there any way we can print the message "Hello" which is there in the class?
Yes, you can load the class in another class that does have a main
method. When your class loads the static initializer block will be executed, and the message will be printed.
Related Topics
Problem with Assigning an Array to Other Array in Java
Help with Packages in Java - Import Does Not Work
How to Tune Tomcat 5.5 Jvm Memory Settings Without Using the Configuration Program
Selenium Webdriver + Java - Eclipse: Java.Lang.Noclassdeffounderror
Does the Sequence of the Values Matter in a JSON Object
How to Open a Windows Named Pipe from Java
Recursively List All Files Within a Directory Using Nio.File.Directorystream;
Generics:List<? Extends Animal> Is Same as List<Animal>
How to Make Anonymous Inner Classes in Java Static
How to Write a Key Listener to Track All Keystrokes in Java
Package a Non-Modular Javafx Application
Java Reflection: How to Get the All Getter Methods of a Java Class and Invoke Them
Java.Util.Concurrentmodificationexception Not Thrown When Expected
How to Solve the 'Classic' Knapsack Algorithm Recursively
Java Interfaces Methodology: Should Every Class Implement an Interface