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

你离开我真会死。 提交于 2019-12-04 07:54:53

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.

lexicore

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 JDK has a different file structure. This is why you have to define these profiles.

See also the following posts:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!