Vscode-java-debug: ClassNotFoundException on adding and running new java file

Created on 12 Jul 2019  路  18Comments  路  Source: microsoft/vscode-java-debug

On adding a new Java file into an existing project and clicking on the Run or Debug text at the top of main function results in ClassNotFoundException. Same issue also happens when manually adding the mainClass entry in the launch.json file.

Workaround: Do a _Full_ Java: Force Java Compilation from the command palette before attempting to Run or Debug the main function. Or, restart/reload VSCode, which appears to trigger a force compilation.

Repro repo: here

Runtime information

JDK: 12

Debugger for Java: 0.20.0

VSCode:

Version: 1.37.0-insider (user setup)
Commit: 46cc0a1670b7047e610aaef1bf84f1190802e9ec
Date: 2019-07-11T05:39:42.001Z
Electron: 4.2.5
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Windows_NT x64 10.0.18362
bug external

Most helpful comment

No clue about the cause. I would suggest you to try VS Code command Java: Clean the Java language server workspace.

All 18 comments

Could you share your user setting java.debug.settings.forceBuildBeforeLaunch? Setting it to true will force building the workspace before launching java program.

It is enabled for both user and workspace.

No clue about the cause. I would suggest you to try VS Code command Java: Clean the Java language server workspace.

Tried Java: Clean the Java language server workspace but problem persists. This is reproducible on two different (Windows) machines.

Any update? Anything else I could try?

Log files: logs.zip

The repo here you provided is just some empty folders, not sure how to reproduce this issue.
Could you provide a sample project and detailed steps to reproduce this issue?

  1. Go to /learning-computer-science/tree/master/src/Miscellaneous/src/Problems/Programming in local computer.
  2. Copy folder MapWordsListToGrid into the same parent Programming folder as MapWordsListToGrid2
  3. Go inside MapWordsListToGrid2\Java and update the package name in the java files to be consistent with the path.
  4. Now click on the Run under MapWordsListToGrid2\Java\Solution.java.

What is your workspace root when you open the folder in VS Code?
learning-computer-science or /learning-computer-science/tree/master/src/Miscellaneous/src/Problems/Programming?

If you could share your current VS Code screenshot, that may help me better understand your user scenario.

image

bug

See the screenshot, i tried in my machine, it works.

I am going to generate a gif that exhibits the problem. Will post soon.

@testforstephen I have a gif here that exhibits the problem.

I see, thanks.

Revisit your previous logfile from https://github.com/microsoft/vscode-java-debug/issues/606#issuecomment-540698617, i found there are some error details. Looks like the builder got some problems when generating .class file into bin directory.

!MESSAGE Error occured while building workspace. Details: 
 message: A class file was not written. The project may be inconsistent, if so try refreshing this project and building it; code: 0; resource: C:/data/devgithub/_my-projects/learning-computer-science/src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch12_ModifiedBinarySearch/P2_CeilingOfANumber/Java/Solution.java;
 message: Solution cannot be resolved to a type; code: 16777218; resource: C:/data/devgithub/_my-projects/learning-computer-science/src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch12_ModifiedBinarySearch/P2_CeilingOfANumber/Java/SolutionTest.java; line: 12
 message: Solution cannot be resolved to a type; code: 16777218; resource: C:/data/devgithub/_my-projects/learning-computer-science/src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch12_ModifiedBinarySearch/P2_CeilingOfANumber/Java/SolutionTest.java; line: 16
 message: Solution cannot be resolved to a type; code: 16777218; resource: C:/data/devgithub/_my-projects/learning-computer-science/src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch12_ModifiedBinarySearch/P2_CeilingOfANumber/Java/SolutionTest.java; line: 16
 message: Solution cannot be resolved to a type; code: 16777218; resource: C:/data/devgithub/_my-projects/learning-computer-science/src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch12_ModifiedBinarySearch/P2_CeilingOfANumber/Java/SolutionTest.java; line: 21
 message: Solution cannot be resolved; code: 570425394; resource: C:/data/devgithub/_my-projects/learning-computer-science/src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch12_ModifiedBinarySearch/P2_CeilingOfANumber/Java/SolutionTest.java; line: 28
 message: A class file was not written. The project may be inconsistent, if so try refreshing this project and building it; code: 0; resource: C:/data/devgithub/_my-projects/learning-computer-science/src/EducativeIo/Courses/GrokkingTheCodingInterview/Ch12_ModifiedBinarySearch/P2_CeilingOfANumber/Java/SolutionTest.java;

The error A class file was not written. The project may be inconsistent, if so try refreshing this project and building it seems a known issue for eclipse. Have googled it, and there are lots of cases for this issue, not sure which one is the root cause for your environment.

https://stackoverflow.com/questions/17154360/eclipse-build-error-a-class-file-was-not-written-the-project-may-be-inconsiste

@manastalukdar i can reproduce it in other project, this issue is duplicated with https://github.com/eclipse/eclipse.jdt.ls/issues/1137. The root cause is the Java language server doesn't refresh the new package to underlying jdt. We're fixing it in eclipse.jdt.ls side. Current workaround is reload VS Code window.

Language Support for Java(TM) by Red Hat 0.51.0 release has fixed this issue.
@manastalukdar Could you try again?

Fixed. Thanks.

So looks like I had this issue again, but I was able to resolve it.
Basically, the folder name should not contain a colon (:). I am not saying that it is just limited to colons, could be that some other symbols in the folder name could cause this issue as well. Hope that this helps others.

try deleting the .vscode folder and run it again

Was this page helpful?
0 / 5 - 0 ratings