Search code examples
javaprogram-entry-point

How to execute a java program without main method in java 1.7 . I know it works in other releases but i want in 1.7


As far as my knowledge without main core java execution that to in java 1.7 is impossible but still if any answer is there means I am looking for the answer. I don't want the class javafx.application to be appended in the program and all I know is we need to use the thread to get the answer


Solution

  • You can do this, though I don't see how this helps you at all.

    public class Main {
        static {
            System.out.println("Hello World");
            System.exit(0);
        }
    }
    

    You can "run" this class without a main method.

    EDIT: This works on Java 7 update 59 and Java 8 update 51.

    $ /opt/jdk1.7.0_79/bin/java -cp . Main
    Hello World
    
    $ /opt/jdk1.8.0_51/bin/java -cp . Main
    Hello World