Skip to content

dependency with com.sun.java.tools causes building error  #3047

Open

Description

@straywarrior

I have a Maven project that uses JDK 1.8. If I add a dependency, such as net.openhft.lang, containing com.sun.java.tools (without specifying the version) to this project, the language server will generate an error "Missing artifact com.sun.java:tools:jar:17.0.6", indicating that it cannot compile.
However, if I compile the project directly using mvn package, there are no errors. I use mvn dependency:tree to find that the version of com.sun.java.tools is just same as my JDK version installed and specified in the system (1.8.0_151).
It seems that the compilation behavior of the language server is different from directly using Maven, as it tries to look for version 17.0.6 (the JDK version embedded in the language server), instead of using version 1.8 in my system.

Environment
  • Operating System: MacOS 11.4
  • JDK version: 1.8.0_151 (Installed in /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home)
  • Visual Studio Code version: 1.76.2
  • Java extension version: v1.16.0
Steps To Reproduce
  1. Create a Maven project with pom.xml
  2. Add a dependency that requires com.sun.java.tools, such as net.openhft.lang (6.6.11 here), into dependencies in pom.xml
  3. Wait for Language Support for Java to reload the project.

sample project: https://github.com/straywarrior/issue_samples/tree/main/vscode-java/maven-test-project
logs:

!ENTRY org.eclipse.jdt.ls.core 4 0 2023-04-06 15:05:37.733
!MESSAGE Error occured while building workspace. Details: 
 message: The container 'Maven Dependencies' references non existing library '/Users/XXX/.m2/repository/com/sun/java/tools/17.0.6/tools-17.0.6.jar'; code: 964; resource: /Users/XXX/maven-test-project;
 message: The project cannot be built until build path errors are resolved; code: 0; resource: /Users/XXX/maven-test-project;
 message: Missing artifact com.sun.java:tools:jar:17.0.6; code: 0; resource: /Users/XXX/maven-test-project/pom.xml; line: 4
Current Result

Language server returns error: Missing artifact com.sun.java:tools:jar:17.0.6

Expected Result

No error should occur.

Additional Informations

mvn dependency:tree output:

[INFO] com.straywarrior:maven-test-project:jar:1.0
[INFO] \- net.openhft:lang:jar:6.6.11:compile
[INFO]    +- org.ow2.asm:asm:jar:5.0.3:compile
[INFO]    +- org.slf4j:slf4j-api:jar:1.7.6:compile
[INFO]    +- com.sun.java:tools:jar:1.8.0_151:system
[INFO]    +- net.openhft:compiler:jar:2.2.0:compile
[INFO]    |  +- (org.slf4j:slf4j-api:jar:1.7.6:compile - omitted for duplicate)
[INFO]    |  +- (com.sun.java:tools:jar:1.8.0_151:system - omitted for duplicate)
[INFO]    |  \- org.kohsuke.jetbrains:annotations:jar:9.0:compile
[INFO]    \- org.xerial.snappy:snappy-java:jar:1.1.1.6:compile

VSCode settings.json

    "java.configuration.runtimes": [
        {
            "name": "JavaSE-1.8",
            "path": "/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home",
            "default": true
        }
    ]

Activity

  1. fbricon commented on Apr 6, 2023

    @fbricon
    Collaborator

    The language server executes Maven in the same JVM it's started with, so basically, the ${java.home} property is resolved with JDK17 in net.openhft:compiler:2.2.0:

    <dependency>
        <groupId>com.sun.java</groupId>
        <artifactId>tools</artifactId>
        <version>${java.version}</version>
        <scope>system</scope>
        <systemPath>${java.home}/../lib/tools.jar</systemPath>
    </dependency>

    Does your project still compile if you exclude tools.jar with:

    <dependency>
        <groupId>net.openhft</groupId>
        <artifactId>lang</artifactId>
        <version>6.6.11</version>
        <exclusions>
         <exclusion>
          <groupId>com.sun.java</groupId>
          <artifactId>tools</artifactId>
         </exclusion>
        </exclusions>
    </dependency>

    ?

  2. straywarrior commented on Apr 6, 2023

    @straywarrior
    Author

    @fbricon Thanks for reply. The answer is yes. If I exclude tools.jar, it can be compiled without error both by Maven and the Language Server. And it is why I can locate the problem.
    In the real situation, com.sun.java.tools is a really "deep" dependency that is included indirectly and I'm not sure whether I can exclude it safely.
    Is it possible to change the environment of Maven in the language server?

  3. sverkera commented on Sep 1, 2025

    @sverkera

    I am running into the same issue and I've tried various ways to work around it but no success. Main issue is that the execution of maven from the plugin is using the JRE of language server, which requires Java 21. Normally ~/.vscode-server/extensions/redhat.java-1.44.0-linux-x64/jre/21.0.8-linux-x86_64/ would be used, but that can be changed by java.jdt.ls.java.home setting. However, even when I set that to a full JDK I get the below error message:

    Project build error: 'dependencyManagement.dependencies.dependency.systemPath' for com.sun:tools:jar refers to a non-existing file /usr/local/sdkman/candidates/java/21.0.8-tem/../lib/tools.jar. Please verify that you run Maven using a JDK and not just a JRE.Java(0)

    The archive tools.jar dissapeared long time ago, I believe already in JDK 9. Issue is that I can't set the execution environment separately for maven plugin.

    When I run maven from command line I have $JAVA_HOME pointing to JDK 8 and then build works correctly. For gradle there is a setting for java home to use but not for maven.

  4. jiangshanwe commented on Mar 7, 2026

    @jiangshanwe

    I have the same error, help

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions