Compiling and running Java code using code runner extension in VSCode

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 and java main, it works fine.. the problem is the extension is not working.

Any help would be appreciated.

Thanks in advance.


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.