I was asked this question in an interview.
How to print message on console without using main() method?
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)