I am getting one weird issue and getting a compile time exception in my pom.xml when i am trying to add dependancy for tools. jar displayed as below(Missing artifact com.sun:tools:jar:1.6.0)
I have set my JAVA_HOME variable as below:
JAVA_HOME: C:\Program Files\Java\jdk1.6.0_34
When i hardcode it to the actual path of JDK1.6 i dont find any error as 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 its not good practise. Request guidance in resolving this error.
java.home
is a System property which generally points to the jre directory and you are getting an error as you have pointed to a jar which doesn't exist.
In case you want to refer to an environment variable within your pom file, use the below syntax.
${env.variable_name}
In your case, it should be ${env.JAVA_HOME}
as seen 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 has mentioned, this wont work with MAC as the MAC JDK has a different file structure.
In Maven, ${java.home} points to the JRE directory used by the JDK, not JDK itself. Please see this question:
Java_home in Maven
So instead of
${java.home}/lib/tools.jar
which assumes the JDK directory you should have used
${java.home}/../lib/tools.jar
However, this is only a half of the solution. The problem is that under Mac, the directory structure is different. You have to user profiles in order to make your build relibaly work cross-platform.
Please see this question:
JDK tools.jar as maven dependency
And, specificaly, this answer (which IS the correct answer and not the one accepted by the OP there).
This is how Oracle handles it in one of their 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>
On Mac, the JDK has a different file structure. This is why you have to define these profiles.
See also the following posts:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With