1

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?

  • is the name of the file that contains the `Main` class really `program.java`? – geocodezip Mar 06 '21 at 16:33
  • check updated question –  Mar 06 '21 at 16:37
  • add `:.` to end of CLASSPATH (unless your `Main.class` is inside of one of the listed `.jar`) –  Mar 06 '21 at 16:51
  • yes Sir! `export CLASSPATH=$PWD/activation-1.1.jar:/usr/local/opt/openjdk/plugins/javax.mail.jar:.` –  Mar 06 '21 at 16:53
  • I've change my class name from `Main` > `ThisIsMyPersonalClass` and added `:.` at the end of the `CLASSPATH`. and updated my source. It worked. but what does that `:.` mean sir? –  Mar 06 '21 at 16:57

1 Answers1

2

Instead of trying to run the Main.class

just do: java Hello.java

Schwencke
  • 46
  • 6
  • but I want to compile then run it. –  Mar 06 '21 at 16:44
  • @Arghadip In recent versions of Java, `java Hello.java` will both compile and run a simple class. By the way, you might enjoy the [REPL](https://en.wikipedia.org/wiki/Read-eval-print_loop) tool for Java, [*jshell*](https://en.m.wikipedia.org/wiki/JShell). – Basil Bourque Mar 06 '21 at 17:33