-4

When I go to the folder with a single java file on Mac and run javac HelloWorld.java, the class compiles correctly, but when running it with the java command, I get the following error:

Error: Could not find or load main class main
Caused by: java.lang.ClassNotFoundException: main

However, in the VSCode terminal, the identical commands work and run the program. What am I doing wrong?

java version: "14.0.1", 2020-04-14

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Nandha
  • 96
  • 7
  • Does this answer your question? [What does "Could not find or load main class" mean?](https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean) – shreyasm-dev Aug 18 '20 at 19:43
  • 2
    How do you run the `java` command? make sure you provide a *fully qualified name* of your compiled file (include packages), and that you don't include a `.class` extension in your `java ClassName` command. – Giorgi Tsiklauri Aug 18 '20 at 19:43
  • Yes, I checked all names and syntax, and I still get the same error – Nandha Aug 18 '20 at 19:47
  • It is impossible that you checked and provided the correct *Fully Qualified Name* without `.class` extension, and it doesn't run, considering, of course, that you have a `public static void main(String[] args) {}` method in your main class. – Giorgi Tsiklauri Aug 18 '20 at 19:48
  • What does `type java` say? – that other guy Aug 18 '20 at 20:48
  • 2
    Can you post the `java` command you used and the code for `HelloWorld.java`? – Nosrep Aug 18 '20 at 20:50
  • We can't help you if you don't post the java command. – shreyasm-dev Aug 18 '20 at 21:18

1 Answers1

0

It's impossible. As you described, your java file named 'HelloWorld.java', and you compiled it through command 'javac HelloWorld.java'. But the error message shows:

Error: Could not find or load main class main
Caused by: java.lang.ClassNotFoundException: main

This means the java interpreter is looking for a class named 'main', not the 'HelloWorld'. So what's the command you had token? If you take command 'java HelloWorld' and the java interpreter can't find 'HelloWorld' class, you should get this error message:

Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13