Search code examples
javajavac

Error: Could not find or load main class HelloWorld


I have created a HelloWorld.java file in Git Bash by

 $vi HelloWorld.java

Then inserted the following:

   class Hello { 
        public static void main (String[] arguments) {              
            System.out.println ("Hello, world!");
       }
    }

push esc and write :wq!

Then I tried to call the program.

  $ls
  HelloWorld.java
  $javac HelloWorld.java
  $ls
  Hello.class HelloWorld.Java
  $ java HelloWorld
  Error: Could not find or load main class HelloWorld
  Caused by: java.lang.ClassNotFoundException: HelloWorld

Can you help me?


Solution

  • You need to fix few things:

    1. Your main class should have public access modifier

      public class Hello {}
      
    2. Also file name and class name should match. So if your class name is Hello, your file should be named Hello.java