redhat-developer / vscode-java

Java Language Support for Visual Studio Code
Eclipse Public License 2.0
2.08k stars 436 forks source link

_ is not on the classpath of project _, only syntax errors are reported #1886

Open Nacandcheese opened 3 years ago

Nacandcheese commented 3 years ago

Every time I try to create a package for my java classes so I can finish my projects for school, errors like this occur: image When I finally try to run the program, I get an even more infuriating message: image It hurts me to be unable to properly test my code, even if I know I did well. It has been giving me anxiety for quite a while now, so I'm quite surprised this has slipped under the radar for so long.

Steps To Reproduce

  1. Create a java class with methods and everything.
  2. Put it in a folder with a test program.
  3. List the folder as a package in both programs.
  4. Try to use the class and it's methods in the test program. Both errors should be clearly visible by now.

Here is my code as an example of what can cause it. Try compiling either Test.java or Customer.java and it should be quite apparent. Chapter10.zip

Current Result

A warning that constantly results in weird compiler errors

Expected Result

A working class that I can use

Nacandcheese commented 3 years ago

Ignore the mcfunction plugin in the image of the versions, the plugin has nothing to do with the issue from what I have tested.

testforstephen commented 3 years ago

Opening a package folder 'Chapter10' as root doesn't work, you must open a parent directory containing the packages.

For example, you could create another root folder such as 'Exercise', and put 'Chapter10' folder under it, then open 'Exercise' in VS Code.

├── Exercise
│   └── Chapter10
│       ├── Test.java
│       └── Person.java
Nacandcheese commented 3 years ago

That is rather odd, I have been running from a Programming 2 folder this entire time and it still refuses to work. image image Even adding another folder wouldn't work. I can't get the customer file to see that it is an extension of Person. It basically will not even read from the files in the package.

abdeali004 commented 3 years ago

In VS Code just right-click on the src (the folder which contains .java files) folder and select the " Add folder to Java source path" option. If it didn't' worked, then try to remove it first by selecting "remove the folder from Java source path" and then adding it again.

Restart once when done. Hope it works for you.

retbleed commented 2 years ago

It keeps telling me to edit the pom.xml file

m1ckr1sk commented 1 year ago

Same issue as @retbleed. Error is "Unsupported operation: Please use pom.xml to manage the source directories of maven projects.". Cleaning the workspace fixed the issue.

brandonros commented 1 year ago

Please use build.gradle file to manage the source directories of gradle project.

when dealing with an Android APK project and trying to do "Add folder to Java source path"

nurrony commented 1 year ago

I was working with multi-module Quarkus Maven project. I was facing the same issue. I just hit Java: Clean Java language server Workspace. After reloading the workspace, the issue was no more.

chientrm commented 1 year ago

Whenever there is pom.xml or build.gradle in my root project, this error happen. And I can't manage an Android project without maven or gradle.

lakshmireddy999 commented 1 year ago

It keeps telling me to edit the pom.xml file i followed this and it worked for me https://stackoverflow.com/questions/9752972/how-to-add-an-extra-source-directory-for-maven-to-compile-and-include-in-the-bui

junlan0050 commented 1 year ago

Press "Ctrl + Shift + P", choose "java : Clean java Language Server workspace". If not run, right click on src, choose "add folder to java source path"

This solution works for me with correct gradle file.

quanghuyle3 commented 11 months ago

Press "Ctrl + Shift + P", choose "java : Clean java Language Server workspace". If not run, right click on src, choose "add folder to java source path"

This solution works for me with correct gradle file.

Press "Ctrl + Shift + P", choose "java : Clean java Language Server workspace". If not run, right click on src, choose "add folder to java source path"

This solution works for me with correct gradle file.

I tried the above solution and it resolved issue successfully. My project is with Maven.

doulikecookiedough commented 7 months ago

Experiencing the issue described here on my machine (MacOS Ventura 13.2.1, JDK17) with a multi-module maven project.

I have tried the following to no avail:

Does anyone have any other suggestions to try?

wmstack commented 7 months ago

I have a problem where I have some Java Workspaces image

And when I click the run button it runs the other projects Main.java. I have to remove the other one from the source path to run the first one, otherwise the wrong one runs even though the run button is on the first Main.java file.

JackER4565 commented 5 months ago

Dumb question, Are the files inside the SRC folder? That was it for me....

shenguanjiejie commented 4 months ago

Experiencing the issue described here on my machine (MacOS Ventura 13.2.1, JDK17) with a multi-module maven project.

I have tried the following to no avail:

  • Adding "java.jdt.ls.java.home": "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home", to my settings.json file
  • Ctrl + Shift + P -> Clean java Language Server workspace
  • Right clicking my src project and clicking Add Folder to Source Path, which throws the error Unsupported operation. Please use pom.xml file to manage the source directories of maven project.
  • Changing my pom.xml by adding a space to force VSCode to reload

Does anyone have any other suggestions to try?

@doulikecookiedough same to me , did you get any solution?

doulikecookiedough commented 4 months ago

@shenguanjiejie Unfortunately no. I abandoned using VSCode for my Java projects, moved onto IntelliJ IDEA Studio (Community Edition). Happy to try out another solution if anyone has any suggestions, but I don't have scope at this time to dig into it and investigate further.

shenguanjiejie commented 4 months ago

@doulikecookiedough I have fixed this problem. check your ~/.m2 directory , If there not have ~/.m2/settings.xml, find it and copy to there. then reload vscode.

drakgoku commented 1 month ago

The solution for me. If you have this structure (with kotlin gradle):

1 - Compile the gradle project even if it is not in the classpath of the project. 2 - Add them to the classpath of the parent: in case of kotlin gradle rootProject.name = "main" include("microservice 1/module","microservice 2/module") 3 - Compile the gradle of the parent (the children will be compiled) 4 - java : Clean java Language Server workspace