Error: Could not find or load main class
See original GitHub issueWhen running the Java program by clicking “Run” on top of the static main function, it reports “Could not find or load the main class: xxx”. However, directly invoking java with the source file works without any error. I tried to use the “Java: Clean the Java Language Server Workspace” command, and error persists.
By looking at the log (, here are two errors reported that may be the cause of the issue:
[2019-10-02 16:48:22.718] [exthost] [error] [redhat.java] provider FAILED
[2019-10-02 16:48:22.720] [exthost] [error] Error: The request (id: 28, method: 'textDocument/hover') has been cancelled
at c:\Users\tingh\.vscode\extensions\redhat.java-0.50.0\dist\extension.js:1:68555
at c:\Users\tingh\.vscode\extensions\redhat.java-0.50.0\dist\extension.js:1:68849
at Immediate.module.exports.M.G.Y.size.setImmediate (c:\Users\tingh\.vscode\extensions\redhat.java-0.50.0\dist\extension.js:1:69210)
at runCallback (timers.js:694:18)
at tryOnImmediate (timers.js:665:5)
at processImmediate (timers.js:647:5)
[2019-10-02 16:48:23.302] [exthost] [info] ExtensionService#_doActivateExtension ms-vscode.cpptools {"startup":false,"activationEvent":"onDebug"}
[2019-10-02 16:48:23.302] [exthost] [info] ExtensionService#loadCommonJSModule file:///c:/Users/tingh/.vscode/extensions/ms-vscode.cpptools-0.25.1/dist/main
[2019-10-02 16:48:23.429] [exthost] [info] ExtensionService#_doActivateExtension vscode.debug-server-ready {"startup":false,"activationEvent":"onDebugResolve"}
[2019-10-02 16:48:23.429] [exthost] [info] ExtensionService#loadCommonJSModule file:///c:/Program Files/Microsoft VS Code/resources/app/extensions/debug-server-ready/dist/extension
Environment
- Operating System: Windows 10 x64 Pro 1903, Build 18362.356
- JDK version: Oracle Java 12.0.2
- Visual Studio Code version: 1.38.1
- Java extension version: 0.50.0
Steps To Reproduce
Sample Project: (FileIO.java)
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class FileIO {
public static void main(String[] args)
throws IOException {
File file = new File("data.txt");
Scanner fileInput = new Scanner(file);
for(int i = 0; i < 5; i++) {
System.out.println(
i + ": " + fileInput.nextDouble()
);
}
fileInput.close();
}
}
Current Result
c:\Research\test_jav>cd C:\Research\test_jav && C:\Users\tingh\.vscode\extensions\vscjava.vscode-java-debug-0.22.0\scripts\launcher.bat "C:\Program
Files\Java\jdk-12.0.2\bin\java" --enable-preview -Dfile.encoding=UTF-8 -cp C:\Users\tingh\AppData\Roaming\Code\User\workspaceStorage\24ee80637ed5c707879e2f0bc2507a43\redhat.java\jdt_ws\jdt.ls-java-project\bin FileIO
Error: Could not find or load main class FileIO
Caused by: java.lang.ClassNotFoundException: FileIO
Expected Result
c:\Research\test_jav>"C:\Program Files\Java\jdk-12.0.2\bin\java" FileIO.java
0: 11.0
1: 22.0
2: 33.0
3: 44.0
4: 55.0
Additional Informations
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8
Top Results From Across the Web
Java Guide: How to Fix "Could not find or load main class"
The Java “Could not find or load main class” error is thrown when the JVM fails to find or load the main class...
Read more >java - What does "Could not find or load main class" mean?
When you get the message "Could not find or load main class ...", that means that the first step has failed. The java...
Read more >Java – “Could Not Find or Load Main Class” Error | Baeldung
And it failed with the error “Could not find or load main class helloworld.” As discussed earlier, the compiler will generate the .class...
Read more >Error: Could not find or load main class in Java [Solved]
If you are getting "Error: Could not find or load main class XXX", where XXX is the name of your main class while...
Read more >Could Not Find or Load Main Class in Java - Javatpoint
Reasons to Occur Error · The class has been declared in the wrong package. · Dependencies missing from the CLASSPATH. · The subdirectory...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was able to reproduce this problem using your files. I coped the source code into
test/FileIO.java
and created the following filetest/data.txt
:I got the same error:
I first noticed this issue today, after upgrading to the latest version of macOS 10.15 Catalina Beta, Build 19A578c. It was with my own project, and the specific error causing the problem (found in the VS Code java logs) was as follows:
VS Code Version Info Version: 1.38.1 Commit: b37e54c98e1a74ba89e03073e5a3761284e3ffb0 Date: 2019-09-11T13:31:32.854Z Electron: 4.2.10 Chrome: 69.0.3497.128 Node.js: 10.11.0 V8: 6.9.427.31-electron.0 OS: Darwin x64 19.0.0
VS Code Extension Info Java Extension Pack Version: 0.8.0 Debugger for Java Version: 0.22.0 Java Test Runner: 0.20.0 Language Support for Java™ by Red Hat: 0.50.0
Java Version Info
I hope this helps to narrow down the problem. I am very inexperienced and Google couldn’t help me interpret the error messages as well as I’d hoped, so I hope someone else can!
If you are using vscode for creating java project and want to run java files as a single java file by
simply remove the package declaration at the top of the file and try it. it shows you error in vscode but it doesn’t have any effect since, it is considered to be a single java file, not project