Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find or load main class - VS Code

I created a new java project (with the option Create Java Project), and When I click the run button (marked with the red circle), it gives me the error Could not find or load main class.

When I run the program from the run option above the main(marked with the green circle), it works and doesn't show me the error.

How can I fix it?

The project

This is the .classpath file:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="output" path="bin"/>
</classpath>
like image 277
Dani Avatar asked Sep 09 '19 16:09

Dani


People also ask

Could not find or load main class in VS code Java?

The primary reason behind why the 'could not find or load main class ' is encountered is because JVM is unable to locate where your . class files were being saved.

Why could not find or load main class?

Reasons to Occur Error The error generates because the JVM fails to load the main class or package name. There are some other reasons that generate the same error, as follows: The class has been declared in the wrong package. Dependencies missing from the CLASSPATH.


4 Answers

This is a known issue https://github.com/microsoft/vscode-java-debug/issues/606, https://github.com/eclipse/eclipse.jdt.ls/issues/1137, which caused the new package not recognized during building workspace. It's expected to be fixed in vscode-java Middle October release.

Current workaround is reload VS Code window, or F1 ->Clean the java language server workspace.

like image 197
jinbo wang Avatar answered Sep 28 '22 03:09

jinbo wang


i think you install the Code Runner Extension,so you have a Run icon in the upper right-hand corner,you could try to uninstall it,

then F1 ->Clean the java language server workspace.,run your code with F5 or the Run above your Main method

like image 31
Leo Zhu - MSFT Avatar answered Sep 28 '22 01:09

Leo Zhu - MSFT


Try editing your .classpath file to show it where your main class is.

An easier, but much more jank method, is to move your .java file to the root of your project directory to help vscode find it.

like image 32
SenorC4 Avatar answered Sep 28 '22 01:09

SenorC4


Install java extension package and try running it by CTRL + F5. Works well by the time it gets fixed.

like image 29
Naufil Avatar answered Sep 28 '22 03:09

Naufil