Cannot resolve com.sun:tools:0 in Maven Project?
Asked Answered
A

1

6

I've been working with test automation a lot lately.

Im now familiarized with several tools, frameworks, dependencies, etc. like Selenium, JUNits, TestNG that helps me manage my daily work as a QA Engineer.

I frequently use Selenium and TestNG to build up my solutions, together with IntelliJ IDEA that runs on the latest JDK version (jdk-15.0.1) available on a Mac running on macOs Catalina Version 10.15.7.

I have mi POM.mxl file already configured for my projects and since the very beginning it displayed the following error on the console:

Cannot resolve com.sun:tools:0

I always ignored that because my projects within my IDE always compiled&ran and because I was never using that dependency. But now I'm trying to run my project with maven support from my Terminal in my Mac and because of the sun:tools.jar dependency, I'm not allowed to compile any working project I have developed so far.

I have read a lot of similar threads on similar problems with the same sun:tools:jar dependency like in:

I have done everything, but many solutions were based on finding the tools.jar file in the jdk installation directory. Since I'm using JDK 15, this dependency was already removed:

Removed: rt.jar and tools.jar

The class and resource files previously stored in lib/rt.jar, lib/tools.jar, lib/dt.jar, and various other internal JAR files are now stored in a more efficient format in implementation-specific files in the lib directory. The format of these files is not specified and is subject to change without notice.

I attach the exact error message I get with the terminal:

 ----------------------< org.example:YelpExercise >----------------------
[INFO] Building YelpExercise 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.737 s
[INFO] Finished at: 2020-11-04T18:59:47-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project YelpExercise: Could not resolve dependencies for project org.example:YelpExercise:jar:1.0-SNAPSHOT: Could not find artifact com.sun:tools:jar:0 at specified path /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home/../lib/tools.jar -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
Awhile answered 4/11, 2020 at 22:0 Comment(4)
tools.jar was removed in JDK9 as you found out. What happens when you remove the dependency from pom.xml and run - what code fails to compile? You may have to start thereWoothen
That's the problem! I don't explicitly add the tools.jar dependency in my pom.xml. I think my IDE does that because it somehow is pointing to my jdk and therefore wants to solve that dependency? I really don't know much about that. I guess there is a way to exclude that particular dependency, but I'm not aware of how to declare this explicit exclusion in my pom.xml file nor set up my IDE so that it understands it shouldn't be looking for tools.jarAwhile
Looks like the tools.jar is required by some Maven goal in your project, but since it was removed from JDK 9+ you would get this error if run Maven under JDK 9+.Babby
Indeed, that was happening all the time. I found the solution and answer my question just a minute ago. Check it out.Awhile
A
8

Ok, after hours and even days of research, I found the solution:

For JDK 9 and above, when defining the pom.xml, you have to exclude that sub-dependency whenever you will use a dependency that contains it. In my case, the cobertura dependency included sun.com:tools.

What I edited in my pom.xml file:

<!-- https://mvnrepository.com/artifact/net.sourceforge.cobertura/cobertura -->
    <dependency>
        <groupId>net.sourceforge.cobertura</groupId>
        <artifactId>cobertura</artifactId>
        <version>2.1.1</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>com.sun</groupId>
                <artifactId>tools</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

This seems to be still an open issue for some maven dependencies. Check: issues1 or issues2 or Google search: <dependency_you_are_using> sun tools for more info.

Awhile answered 5/11, 2020 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.