Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassNotFoundException on adding and running new java file #606

Closed
manastalukdar opened this issue Jul 12, 2019 · 22 comments
Closed

ClassNotFoundException on adding and running new java file #606

manastalukdar opened this issue Jul 12, 2019 · 22 comments

Comments

@manastalukdar
Copy link

manastalukdar commented Jul 12, 2019

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
@testforstephen
Copy link
Contributor

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

@manastalukdar
Copy link
Author

It is enabled for both user and workspace.

@testforstephen
Copy link
Contributor

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

@manastalukdar
Copy link
Author

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

@manastalukdar
Copy link
Author

manastalukdar commented Oct 10, 2019

Any update? Anything else I could try?

Log files: logs.zip

@testforstephen
Copy link
Contributor

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?

@manastalukdar
Copy link
Author

  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.

@testforstephen
Copy link
Contributor

testforstephen commented Oct 11, 2019

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.

@manastalukdar
Copy link
Author

image

@testforstephen
Copy link
Contributor

bug

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

@manastalukdar
Copy link
Author

manastalukdar commented Oct 11, 2019

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

@manastalukdar
Copy link
Author

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

@testforstephen
Copy link
Contributor

I see, thanks.

Revisit your previous logfile from #606 (comment), 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

@testforstephen
Copy link
Contributor

testforstephen commented Oct 12, 2019

@manastalukdar i can reproduce it in other project, this issue is duplicated with eclipse-jdtls/eclipse.jdt.ls#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.

@testforstephen
Copy link
Contributor

testforstephen commented Oct 17, 2019

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

@manastalukdar
Copy link
Author

Fixed. Thanks.

@ghost
Copy link

ghost commented Jun 3, 2021

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.

@hnguyenboy2001
Copy link

try deleting the .vscode folder and run it again

@mohamed-badr555
Copy link

welcome ,sir, this error appears to me when I run code
Error: Could not find or load main class Main
Caused by: java.lang.NoClassDefFoundError: videos/ClassAndObjects/Main (wrong name: Main)

@Lexiphan
Copy link

I vote here too, the problem still persists on my VS Code with the analogous message as in previous post.

@MariHGU
Copy link

MariHGU commented May 20, 2023

This issue happens to me too. It's like viscose doent recognize when I create a file. No package is given, if I use source action to create functions, it creates a new file with only functions, no class, no package etc.

@abhishek01-github
Copy link

#692 (comment)
Go for this solution, it works!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants