Printing message on Console without using main() method
public class Foo {
static {
System.out.println("Message");
System.exit(0);
}
}
The System.exit(0)
exits program before the jvm starts to look for main()
(Note: This works only with java 6. Even if it compiles with JDK 7's javac
it cannot be run with its java
, because it expects a main(String[])
method.)
Is it possible to print message on console without main and static block in java?
You could define a custom class loader that prints your message :
public class MyClassLoader extends ClassLoader {
public MyClassLoader(ClassLoader other) {
super(other);
System.out.println("Hi there");
System.exit(0);
}
}
Then run the java command :
java -Djava.system.class.loader=MyClassLoader
(don't need to add a class as parameter)
while printing some statements in console without main function using static block
There is an exception for classes that extend javafx.application.Application
- you don't need to define the main
method for them.
The java command can be used to launch a JavaFX application by loading a class that either has a
main()
method or that extends thejavafx.application.Application
. In the latter case, the launcher constructs an instance of the Application class, calls itsinit()
method, and then calls thestart(javafx.stage.Stage)
method.
Otherwise, in order to start a Java application, the main
has to be defined.
Static blocks are executed when their class is being loaded into the JVM. For a class to be loaded, there should be a trigger/launcher somewhere.
What to do without System.out, to print on console?
You could bypass the System object if you want to. System.out does a lot of extra stuff (handling unicode, for instance), so if you really want just the raw output and performance, you actually probably even should bypass it.
import java.io.*;
public class PrintOutTest {
public static void main(String args[]) throws IOException {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(FileDescriptor.out), "ASCII"), 512);
out.write("test string");
out.write('\n');
out.flush();
}
}
This has been elaborated a bit further in here.
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.
Related Topics
How to Clone a Generic List in Java
Apache Spark - Foreach VS Foreachpartition When to Use What
Java Erasure with Generic Overloading (Not Overriding)
What Does a Jvm Have to Do When Calling a Native Method
How Are Servlet Url Mappings in Web.Xml Used
Spring Scheduling Task - Run Only Once
Rethrowing an Exception: Why Does the Method Compile Without a Throws Clause
How to Search in a List of Java Object
Scanner Class Skips Over Whitespace
Getting Java.Lang.Reflect.Invocationtargetexception While Adding a Button to Layout
Post Increment Operator Not Incrementing in for Loop
Why Are Variables Declared with Their Interface Name in Java
Best Way to Create a Hashmap of Arraylist
Maximum Size of a Method in Java