user13
user13

Reputation: 391

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?

Upvotes: 1

Views: 3831

Answers (2)

Sandy
Sandy

Reputation: 1

$ls
Hello.java

$java Hello.java
Hello, World!

Upvotes: 0

bhusak
bhusak

Reputation: 1390

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

Upvotes: 5

Related Questions