Maven: Missing artifact com.sun: tools: jar: 1.6.0 compile-time exception in POM.xml

I get one weird problem and get an exception at compile time in my pom.xml when I try to add a dependency for tools. ( Missing artifact com.sun: tools: jar: 1.6.0 )

Issue

I set the JAVA_HOME variable as shown below:

JAVA_HOME : C: \ Program Files \ Java \ jdk1.6.0_34

When I rigidly bind it to the real path of JDK1.6, I find no error, as shown below.

<dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.6.0</version> <scope>system</scope> <systemPath>C:\Program Files\Java\jdk1.6.0_34\lib\tools.jar</systemPath> </dependency> 

but I know this is not a good practice. Request recommendations for resolving this error.

+5
source share
2 answers

java.home is a system property that usually points to the jre directory, and you get an error because you pointed to a jar that does not exist.

If you want to access the environment variable in your pom file, use the syntax below.

 ${env.variable_name} 

In your case, it should be ${env.JAVA_HOME} , as shown below

 <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.6.0</version> <scope>system</scope> <systemPath>${env.JAVA_HOME}/lib/tools.jar</systemPath> </dependency> 

Update:. As lexicore noted, this does not work with MAC, as the MAC JDK has a different file structure.

+13
source

In Maven, $ {java.home} points to the JRE directory used by the JDK, not the JDK itself. Please check out this question:

Java_home in Maven

So instead

 ${java.home}/lib/tools.jar 

which suggests a JDK directory that you should use

 ${java.home}/../lib/tools.jar 

However, this is only half the solution. The problem is that on Mac the directory structure is different. You have user profiles to make your relibaly build cross-platform.

Please check out this question:

JDK tools.jar as a maven dependency

And, specifically, this answer (this is the correct answer, not the one accepted by the OP there).

This is how Oracle processes it in one of its POMs :

 <!-- JDK dependencies --> <dependency> <groupId>com.sun</groupId> <artifactId>tools</artifactId> <version>1.6</version> <scope>system</scope> <systemPath>${tools.jar}</systemPath> </dependency> 

And then in profiles :

 <profile> <id>default-tools.jar</id> <activation> <file> <exists>${java.home}/../lib/tools.jar</exists> </file> </activation> <properties> <tools.jar>${java.home}/../lib/tools.jar</tools.jar> </properties> </profile> <profile> <id>default-tools.jar-mac</id> <activation> <file> <exists>${java.home}/../Classes/classes.jar</exists> </file> </activation> <properties> <tools.jar>${java.home}/../Classes/classes.jar</tools.jar> </properties> </profile> 

The Mac JDK has a different file structure. That is why you must define these profiles.

See also the following messages:

+2
source

Source: https://habr.com/ru/post/1204909/


All Articles