question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Could not find or load main class

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

1reaction
rajkunducommented, Oct 3, 2019

I was able to reproduce this problem using your files. I coped the source code into test/FileIO.java and created the following file test/data.txt:

11.0
22.0
33.0
44.0
55.0

I got the same error:

Rajs-MacBook-Pro:~/Desktop/test raj$ /Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/bin/java --enable-preview -Dfile.encoding=UTF-8 -cp "/Users/raj/Library/Application Support/Code/User/workspaceStorage/afc777b9c5a0a53b50d81c4dcd540ac6/redhat.java/jdt_ws/test_196f9c6f/bin" FileIO 
Error: Could not find or load main class FileIO
Caused by: java.lang.ClassNotFoundException: FileIO

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:

!ENTRY org.eclipse.jdt.ls.core 4 0 2019-10-03 00:48:33.995
!MESSAGE Error occured while building workspace. Details: 
 message: Preview features enabled at an invalid source release level 12, preview can be enabled only at source level 13; code: 2098258; resource: /Users/raj/Box/Compsci 201/projects/p2-markov-wordgram/src/SimpleWordGramDriver.java;

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

Rajs-MacBook-Pro:~ raj$ java --version
java 12.0.1 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

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!

0reactions
feyselmubarekcommented, Nov 24, 2019

If you are using vscode for creating java project and want to run java files as a single java file by

javac your_class.java java your_class

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

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found