1

I just started learning Java and am using VS Code. For compiling and running the program I use the code runner extension and works perfectly fine for C++ and Python. But in Java the run button is not that of the extension's and the shortcut for the extension (Ctrl+Alt+N) returns an error. I have downloaded JDK version 16 from Oracle and the have included its path to the environment variables as well. The error it shows is:

Error: Could not find or load main class main

Caused by: java.lang.NoClassDefFoundError: Main (wrong name: main)

When running by doing javac main.java and java main, it works fine.. the problem is the extension is not working.

Any help would be appreciated.

Thanks in advance.

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • Click **Run Java**, this is provided by Java extension. Does the error still exists? – Molly Wang-MSFT Sep 17 '21 at 09:07
  • 1
    that does work but is there any keybinding for that? Also I'm just curious as to why code runner doesn't work as the some others I know have it working out of the box... didn't have to modify anything.. – Nikhil John Jose Sep 17 '21 at 09:35
  • Ctrl+F5. You may compare the execution scripts provided by Code Runner and Java Extension, code runner may miss some packages. It's recommended you use Java extension, it has more powerful features. – Molly Wang-MSFT Sep 17 '21 at 09:58
  • I'll turn my comment to an answer, if it solves your question, it's appreciated that you [accept](https://stackoverflow.com/help/someone-answers) it to mark your question as resolved. – Molly Wang-MSFT Sep 17 '21 at 10:04

1 Answers1

0

Click Run Java, the error should go away.

Comparing the execution scripts provided by Code Runner and Java Extension, the one in code runner may miss some packages, which caused the error.

It's recommended you use Java extension Pack, it has more powerful features. Ctrl+F5 is the keyboard shortcuts for Run without Debugging, you can use it to run java code.

Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22