'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>


Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

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

Solution 5:[5]

If none of the solutions worked, simply re-build your project to generate updated .class file for the class that contains the main method.

Basically, this error means that Java compiler was unable to find the proper .class file, and your source .java has a main method that wasn't added to the compiled .class yet.

This can be the case mainly for Maven projects.

Solution 6:[6]

Hey there was still having the same issue. My solution was to compile the code from the command line. This made the .class file for me, and then I was able to run my code. Probably better solutions out there but that's what I did

Solution 7:[7]

I think this is because there isn't a 'Main' Class, and the compiler is looking for that specific class, might be an issue with the project manager naming the class App instead of 'Main', a quick fix might be to name the Class 'Main' and see if that works instead.

Solution 8:[8]

Pressing ctrl+f5 solves the problem.

enter image description here

Terminal output:

PS F:\Testing>  & 'c:\Users\xyz\.vscode\extensions\vscjava.vscode-java-debug-0.34.0\scripts\launcher.bat' 'C:\jdk-18\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\xyz\AppData\Roaming\Code\User\workspaceStorage\1edb6264f8da6da9dda20a68800df89b\redhat.java\jdt_ws\Testing_e004a758\bin' 'mypackage.Foo'
hello

Solution 9:[9]

try deleting the .vscode folder and run it again

Solution 10:[10]

The same happened for me but it was easy fix, Check the launch.json if the class name is correct

Since I changed the group of my class.

Check wheter your launch.json file data is correct. Since sometimes launch.json does not auto-update.

Hope it helps.

Solution 11:[11]

In my case, I added the launch.json in the project, and modified the default configs from ${file} like this: enter image description here

then I started debugging and it worked as usual.

Solution 12:[12]

If none of the above works for you, check your folder/file name. See if there is any non-English naming.

My issue was I can compile and run the program with no problem in terminal, but got the error when running in VScode. Solve the problem by renaming my file path from /Documents/??/App.java to /Documents/Algo/App.java

Solution 13:[13]

Create a file settings.json inside the .vscode folder of your application and add below configuration.

"java.configuration.updateBuildConfiguration": "automatic"