vscode-java icon indicating copy to clipboard operation
vscode-java copied to clipboard

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

Open Nacandcheese opened this issue 4 years ago • 19 comments

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.

  • Operating System: image

  • JDK version: 15.0.2

  • Visual Studio Code version: image

  • Java extension version: image image

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 avatar Apr 16 '21 17:04 Nacandcheese

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

Nacandcheese avatar Apr 16 '21 17:04 Nacandcheese

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

testforstephen avatar Apr 19 '21 08:04 testforstephen

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.

Nacandcheese avatar Apr 20 '21 19:04 Nacandcheese

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.

abdeali004 avatar May 14 '21 06:05 abdeali004

It keeps telling me to edit the pom.xml file

retbleed avatar Apr 06 '22 02:04 retbleed

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.

m1ckr1sk avatar Oct 28 '22 22:10 m1ckr1sk

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"

brandonros avatar Dec 05 '22 07:12 brandonros

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.

nurrony avatar Dec 26 '22 11:12 nurrony

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.

chientrm avatar Feb 13 '23 17:02 chientrm

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

lakshmireddy999 avatar May 08 '23 12:05 lakshmireddy999

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.

junlan0050 avatar Aug 19 '23 16:08 junlan0050

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.

quanghuyle3 avatar Nov 01 '23 00:11 quanghuyle3

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 avatar Feb 22 '24 17:02 doulikecookiedough

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.

wmstack avatar Feb 25 '24 08:02 wmstack

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

JackER4565 avatar May 14 '24 01:05 JackER4565

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?

shenguanjiejie avatar May 23 '24 11:05 shenguanjiejie

@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.

doulikecookiedough avatar May 23 '24 20:05 doulikecookiedough

@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.

shenguanjiejie avatar May 24 '24 08:05 shenguanjiejie