简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM