Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 11 doesn't run main method if the public class is not declared first

Tags:

java

I created the following Chimpazee.java file with the following content:

class Primate {
    public Primate() {
        System.out.print("Primate-");
    }
}

class Ape extends Primate {
    public Ape(int fur) {
        System.out.print("Ape1-");
    }
    public Ape() {
        System.out.print("Ape2-");
    }
}

public class Chimpazee extends Ape {
    public Chimpazee() {
        super(2);
        System.out.print("Chimpazee-");
    }
    public static void main(String[] args) {
        new Chimpazee();
    }
}

However, when I try to execute this file on Windows 10 + PowerShell using Java 11...

PS C:\projects\ocp-java-se\java-se-11\chapter8> java -version
java version "11.0.11" 2021-04-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)

I'm getting a runtime error with the following command

PS C:\projects\ocp-java-se\java-se-11\chapter8> java .\Chimpazee.java
error: can't find main(String[]) method in class: Primate

If I move the Chimpazee class to the beginning of the file it works fine. Isn't suppose to Java compile this independent of the sequence of the classes?

like image 870
João Pedro Schmitt Avatar asked Jul 08 '21 19:07

João Pedro Schmitt


People also ask

Can main method be in non public class?

This is the access modifier of the main method. It has to be public so that java runtime can execute this method. Remember that if you make any method non-public then it's not allowed to be executed by any program, there are some access restrictions applied. So it means that the main method has to be public.

Can a public static void main () method be declared in non public class?

Yes, we can declare the main method as private in Java. It compiles successfully without any errors but at the runtime, it says that the main method is not public.

What does no public class found to execute?

This program for Garbage Collection in Java is not working and showing error that"no public class found to execute" The class with main() needs to be public. No keyword means that it's package private: only classes that are in same package can access it.

How do I fix error main method not found in class?

Here you have to run the class "Main" instead of the class you created at the start of the program. To do so pls go to Run Configuration and search for this class name"Main" which is having the main method inside this(public static void main(String args[])). And you will get your output.


1 Answers

JEP 330 mentions it as:

The class to be executed is the first top-level class found in the source file. It must contain a declaration of the standard public static void main(String[]) method.

i.e. you need to make public class Chimpanzee as the top-level class as follows:

public class Chimpanzee extends Ape {
    public Chimpanzee() {
        super(2);
        System.out.print("Chimpanzee-");
    }
    public static void main(String[] args) {
        new Chimpanzee();
    }
}

class Primate {
    public Primate() {
        System.out.print("Primate-");
    }
}

class Ape extends Primate {
    public Ape(int fur) {
        System.out.print("Ape1-");
    }
    public Ape() {
        System.out.print("Ape2-");
    }
}

Then, run it as

java Chimpanzee.java

Output:

Primate-Ape1-Chimpanzee-
like image 62
Arvind Kumar Avinash Avatar answered Oct 21 '22 07:10

Arvind Kumar Avinash