vscode-java: Project does not build due to build path being incomplete

None of my Java projects are being built properly by the language server.

All my projects are failing, even previously working projects

Fundamentally, I’m getting JRE errors such as The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files.

I’m unable to open standalone Java files, vanilla Java projects and packages, or Maven projects properly.

Here are some things I’ve tried:

  • Cleaned and restarted the language server workspace multiple times.
  • Set up the runtimes properly according to the guide.
  • Set JAVA_HOME, java.home to jEnv symlinks .jenv/versions/14.0 and directly to the JDK installations themselves /usr/local/Cellar/openjdk/14.0.1/.
  • Repeated the above steps with Java 11 and Java 14.
  • Repeated the above steps with Java 11 Homebrew Cask installation of OpenJDK /Library/Java/JavaVirtualMachines/...
  • Full wipe of VSCode and reinstalled everything, with the same results.
Additional Informations

I’m using jenv to manage my JDKs, and I’ve enabled the export and maven plugins. Compilation with javac and running with java works just fine.

Environment
  • Operating System: macOS 10.15.6
  • JDK version: 11.0.8, 14.0.1
  • Visual Studio Code version: 1.48.2
  • Java extension version: 0.65.0
Steps To Reproduce
  1. Open a new .java file and define a class.
Log files

redhat.java/client.log.2020-08-26 client.log

Current Result

Plugin is unable to find JRE classes which causes compilation errors for the server.

Expected Result

Plugin should be able to link to JRE classes and compile properly, providing Intellisense without any errors.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 9
  • Comments: 21 (2 by maintainers)

Most upvoted comments

In VSCode open Command Palette: Java: Clean Java Language Server Workspace -> then restart VSCode fixed this issue for me

I had the same problem, and I’m also using Homebrew-installed OpenJDKs. After some trial and error, finally I found the solution: just point OpenJDK to /usr/local/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home instead of /usr/local/opt/openjdk@11. So the Java runtime configuration looks like this:

"java.configuration.runtimes": [
  {
    "name": "JavaSE-1.8",
    "path": "/usr/local/opt/openjdk@8/libexec/openjdk.jdk/Contents/Home",
  },
  {
    "name": "JavaSE-11",
    "path": "/usr/local/opt/openjdk@11/libexec/openjdk.jdk/Contents/Home",
    "default": true,
  },
  {
    "name": "JavaSE-17",
    "path": "/usr/local/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home",
  },
  {
    "name": "JavaSE-18",
    "path": "/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home",
  },
],

Note: It’s recommended to point OpenJDK to /usr/local/opt/openjdk@11 instead of /usr/local/Cellar/openjdk@11/11.0.5 because the former is always a symlink to the latest version of the formula (in this case, /usr/local/Cellar/openjdk@11/11.0.5) and will survive future minor and patch version updates.

Note 2: If you are just using java, javac, jar or other binaries from the the OpenJDK on the command line, you can just export JAVA_HOME=/usr/local/opt/openjdk but for VS Code to make full use of the OpenJDK you need to point it to /usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home because that’s where the jmods directory resides.

@bryanmylee Could you try the following:

  • stop VS Code
  • run
rm -rf "/Users/bryan/Library/Application Support/Code/User/globalStorage/redhat.java"
rm -rf "/Users/bryan/Library/Application Support/Code/User/workspaceStorage"
  • start VS Code

It’s worked for me add this two props on my settings.json:

   ...


  "java.jdt.ls.java.home": "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home",
  "java.configuration.runtimes": [
    {
      "name": "JavaSE-17",
      "path": "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home"
    }
  ]

In my case the resolution was something different. My project needs Java 8 and pom.xml has this.

<plugin>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.7.0</version>
	<configuration>
		<source>1.8</source>
		<target>1.8</target>
	</configuration>
</plugin>

I have both JDK 8 and 15 installed and VS Code correctly detects both.

image

However, when the project was opened in VS Code it did not choose the JDK correctly to compile the project. I had to manually fix this.

image

After that the project compiled fine and the The project was not built since its build path is incomplete error went away.

It is worth noting that VS Code doesn’t have this problem if you specify the JDK version using properties:

<properties>
	<maven.compiler.source>1.8</maven.compiler.source>
	<maven.compiler.target>1.8</maven.compiler.target>
</properties>

We are switching to this from setting configuration of the maven-compiler-plugin.

@luangong Thanks, that solved my issue. I had to do this:

  "java.configuration.runtimes": [
    {
      "name": "JavaSE-11",
      "path": "/opt/homebrew/Cellar/openjdk@11/11.0.18/libexec/openjdk.jdk/Contents/Home",
    },
    {
      "name": "JavaSE-17",
      "path": "/opt/homebrew/Cellar/openjdk@17/17.0.6/libexec/openjdk.jdk/Contents/Home",
      "default": true
    },
    {
      "name": "JavaSE-19",
      "path": "/opt/homebrew/Cellar/openjdk/19.0.1/libexec/openjdk.jdk/Contents/Home",
    }
  ],

It’s worked for me add this two props on my settings.json:

   ...


  "java.jdt.ls.java.home": "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home",
  "java.configuration.runtimes": [
    {
      "name": "JavaSE-17",
      "path": "/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home"
    }
  ]

The above also worked for me. Does anyone know why this error suddenly occurred sometime late yesterday (UTC/GMT +2 hours)?

<properties>
	<maven.compiler.source>1.8</maven.compiler.source>
	<maven.compiler.target>1.8</maven.compiler.target>
</properties>

Thanks for this! Like many other people, my project builds fine on the command line but the Redhat plugin trips over it. Setting the properties helped me.