Printing Message on Console Without Using Main() Method

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 the javafx.application.Application. In the latter case, the launcher constructs an instance of the Application class, calls its init() method, and then calls the start(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



Leave a reply



Submit