Search code examples
javaprogram-entry-point

Why does the compiler say "main class could not be found or loaded" when main(String[] args) is a method?


Take this code for instance

public class Hello
    {
        static void main(String[] args)
        {
            System.out.println("Hello World");
        }
    }

Here, I didn't write public for the main method and compiled the class. When I run the program why does the error read as "Could not find or load main class Hello.java".

My question is, if main(String[] args) is a 'method' then why say 'main class'?The point is not that public is there or not. The point is that I changed access modifier which caused main(string[] args) to be invisible to JVM. So why does JVM say main class and not main() method?

P.S. If this a stupid question then I really regret asking it.

Edit:- Here is the error message

Error: Could not find or load main class Hello.java

Solution

  • There are two problems here.

    1. main() must be declared as public static void.
    2. However the real problem was your command line. Clearly it was

      java Hello.java
      

      It should have been

      java Hello
      

      There is no such class here as Hello.java. The name of the class is Hello.