Search code examples
javaterminaljarmacos-big-surzshrc

Class exists but says "java.lang.ClassNotFoundException"


I'm trying to compile and run a basic hello word java program on macOS terminal, It's successfully compiling but when I run it, it says java.lang.ClassNotFoundException: Main. It was working before but suddenly stopped working.

Hello.java

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

~/.zshrc

export PATH=/usr/local/opt/openjdk/bin:$PATH
export CLASSPATH=$PWD/activation-1.1.jar:/usr/local/opt/openjdk/plugins/javax.mail.jar

terminal

arghadip@Mac % rm *.class; ls; javac Hello.java; ls; java Main;
Hello.java
Hello.java  Main.class
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
arghadip@Mac % 

I've installed java with brew install java. What went wrong?


Solution

  • Instead of trying to run the Main.class

    just do: java Hello.java