AFAIK, this change was specific to Java 7. In Java 8, you can do this. You cannot do this in Java 7 because it is looking for a method without loading the first class that fails. In any case, it has been changed to Java 8.
public class Main { static { System.out.println("Without main class!!! with " + System.getProperty("java.version")); System.exit(0); } }
prints
Without main class!!! with 1.8.0_66
Note: this will kill the entire program. If you want the program to continue to work without the core, you can do it
public class Main { static { // do something which starts threads System.out.println("Without main class!!! with " + System.getProperty("java.version")); if (true) throw new ThreadDeath(); } }
This will prevent the error message from appearing, but leave the background thread running if there is a thread without a daemon.
Peter Lawrey Feb 29 '16 at 12:24 2016-02-29 12:24
source share