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?
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>
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.
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.
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.
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
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.
Install java
extension package and try running it by CTRL
+ F5
. Works well by the time it gets fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With