5

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)

Issue

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.

sTg
  • 3,848
  • 12
  • 60
  • 105
  • Maven will use the JAVA_HOME env variable to dtermine the JDK to use. Check if that one is set correctly. – Stijn Geukens Oct 17 '14 at 09:45
  • @Shirish, are you running maven command line or from an IDE? – Stijn Geukens Oct 17 '14 at 09:50
  • @StijnGeukens : I am running it through my command prompt but have integrated it with my eclipse. – sTg Oct 17 '14 at 09:52
  • @StijnGeukens : Does that make any difference Stijin as when i run my commands through my command prompt like clean,compile,test i dont find any errors. Should i consider everything fine?. I have integrated my maven project with eclipse for the first time so i am a bit confused about this compile time error. – sTg Oct 17 '14 at 10:06
  • yes this might make a difference if in your eclipse you have defined other settings for the M2E plugin (I don't use eclipse anymore so I can't verify this now). – Stijn Geukens Oct 17 '14 at 11:16
  • Why do you need tools.jar ? – khmarbaise Oct 17 '14 at 18:52
  • @Shirish Sorry to post it here but I am not sure if it was you or someone else who decided to remove my comments which ware reply to your [previous comment](http://stackoverflow.com/questions/26742386/edited-java-add-digits-of-4-numbers/26742697?noredirect=1#comment42084825_26742697). Anyway in case someone would remove them again before you could read them you can find its copies [here](http://i.stack.imgur.com/MEbO6.png) or [here](http://pastebin.com/Fs7f51Z4). – Pshemo Nov 06 '14 at 01:26

2 Answers2

13

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.

coderplus
  • 5,403
  • 4
  • 29
  • 51
3

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:

johnlinp
  • 575
  • 3
  • 15
lexicore
  • 39,549
  • 12
  • 108
  • 193
  • Notice the difference here - they use `${java.home}/../lib` instead of `${java.home}/lib`, which suggests that `${java.home}` points to the `jre` directory inside the JDK rather than the JDK itself. – Ian Roberts Oct 17 '14 at 10:23
  • @IanRoberts Interesting. I wonder why it works then. And this may be also the cause of another problem I was facing: http://stackoverflow.com/questions/26393332/getting-the-pom-for-name-is-invalid-transitive-dependencies-if-any-will-no I'll ask them. – lexicore Oct 17 '14 at 10:46
  • @IanRoberts I have rechecked this. In Maven, ${java.home} points to the jre directory. So the configuration above is correct. See also http://stackoverflow.com/questions/15279586/java-home-in-maven – lexicore Oct 18 '14 at 15:48