JDK tools.jar as maven dependency

JavaMaven 2tools.jar

Java Problem Overview


I would like to put JDK tools.jar as compile dependency. I found some examples that indicate to use the systemPath property like the following:

<dependency>
  <groupId>com.sun</groupId>
  <artifactId>tools</artifactId>
  <scope>system</scope>
  <systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>

The problem is that the path is not correct for Mac Os X (however it is correct for Windows and Linux). For it, the correct path is ${java.home}/../Classes/classes.jar.

I am looking for a way in order to define a maven property such that if system is detected as Mac Os X, value is set to ${java.home}/../Classes/classes.jar, otherwise it is set to ${java.home}/../lib/tools.jar (like it is possible to do with ANT). Does someone has an idea ?

Java Solutions


Solution 1 - Java

That's what profiles are for, extract the path to a property, setup profiles for windows, OSX, etc, and define the property values appropriately.

Here's the doc page that discussing profiles for OSes: Maven Local Settings Model

It should endup looking something like this:

  <profiles>
    <profile>
      <id>windows_profile</id>
      <activation>
        <os>
          <family>Windows</family>
        </os>
      </activation>
      <properties>
        <toolsjar>${java.home}/../lib/tools.jar</toolsjar>
      </properties>
    </profile>
    <profile>
      <id>osx_profile</id>
      <activation>
        <os>
          <family>mac</family>
        </os>
      </activation>
      <properties>
        <toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
      </properties>
    </profile>
  </profiles>

Solution 2 - Java

Thank you for introducing me maven profiles.

I have used profile as mentioned above and by activating a profile based on the presence of the desired file :

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

I posted this answer to highlight a mistake in the previous post : the property section can only be used in activation section in order to activate a profile based on the existence of the specified property. In order to define a property, the properties section must be used like above.

Solution 3 - Java

Hi I know you guys are all smart, but it caused me couple of days to figure out the answer is not complete - both the profile and the dependency is necessary. I hope no one will waste time on this again. Please see my complete code below:

<profiles>
	<profile>
		<id>osx_profile</id>
		<activation>
			<activeByDefault>false</activeByDefault>
			<os>
				<family>mac</family>
			</os>
		</activation>
		<properties>
			<toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
		</properties>
		<dependencies>
			<dependency>
				<groupId>com.sun</groupId>
				<artifactId>tools</artifactId>
				<version>1.6.0</version>
				<scope>system</scope>
				<systemPath>${toolsjar}</systemPath>
			</dependency>
		</dependencies>
	</profile>
</profiles>

Solution 4 - Java

I found a solution in Q: https://stackoverflow.com/questions/35240134/declare-maven-dependency-on-tools-jar-to-work-on-jdk-9/35244168#35244168

>As the actual maven wizardry is quite elaborate, surprising to newcomers and a subject of future improvements, it is better not co copy-paste it around. Hence this module exists so you do not have to know or care about the details. ~~ https://github.com/olivergondza/maven-jdk-tools-wrapper

<dependency>
  <groupId>com.github.olivergondza</groupId>
  <artifactId>maven-jdk-tools-wrapper</artifactId>
  <version>0.1</version>
</dependency>

Solution 5 - Java

Somehow, the eclipse in windows fails to pick up {java.home}. So, I had to set JAVA_HOME instead of java.home. JAVA_HOME was set in Run->Run Configurations->Environment. This worked for me with standard JDK(not Apple JDK).

<profiles>
		<profile>
			<id>windows-profile</id>
			<activation>
				<activeByDefault>true</activeByDefault>
				<file>
					<exists>${JAVA_HOME}/lib/tools.jar</exists>
				</file>
			</activation>
			<properties>
				<toolsjar>${JAVA_HOME}/lib/tools.jar</toolsjar>
			</properties>
		</profile>
		<profile>
			<id>mac-profile</id>
			<activation>
				<activeByDefault>false</activeByDefault>
				<file>
					<exists>${java.home}/../lib/tools.jar</exists>
				</file>
			</activation>
			<properties>
				<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
			</properties>
		</profile>
	</profiles>
    

    <dependencies>
        <dependency>
    			<groupId>jdk.tools</groupId>
    			<artifactId>jdk.tools</artifactId>
    			<version>jdk1.8.0</version>
    			<scope>system</scope>
    			<systemPath>${toolsjar}</systemPath>
    		</dependency>
    	</dependencies>

Solution 6 - Java

The comment of Edward is correct.

You need the profile AND you need the dependency outside of the profiles block. The profile just determines which value ${toolsjar} is gonna get.

<dependencies>
	<dependency>
		<groupId>jdk.tools</groupId>
		<artifactId>jdk.tools</artifactId>
		<version>jdk1.8.0</version>
		<scope>system</scope>
		<systemPath>${toolsjar}</systemPath>
	</dependency>
</dependencies>

Solution 7 - Java

Proper instructions for beginners

First Add this profile to Pom.xml file above tag or somewhere else in it.

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

then Correct JRE path

Goto :

> Windows > Preferecnes > Installed JREs

selected intalled JRE and double click on it or from right menu click edit and then make sure JRE Home path is inside JDK something like:

> C:\Program Files\Java\jdk1.8.0_181\jre

if you have installed JRE seperatly then eclipse would have picked standalone JRE like:

> C:\Program Files\Java\jre1.8.0_181\

so change it to JRE which come with JDK:

> C:\Program Files\Java\jdk1.8.0_181\jre

Solution 8 - Java

my solution:

  1. put the Sun's tools.jar to the $JAVA_HOME/lib
  2. make a symlink in the $JAVA_HOME/.. named lib where target will be $JAVA_HOME/lib

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLaurentView Question on Stackoverflow
Solution 1 - JavasblundyView Answer on Stackoverflow
Solution 2 - JavaLaurentView Answer on Stackoverflow
Solution 3 - JavaJianyuView Answer on Stackoverflow
Solution 4 - Javauser7610View Answer on Stackoverflow
Solution 5 - JavaAnimesh SharmaView Answer on Stackoverflow
Solution 6 - JavaMartyView Answer on Stackoverflow
Solution 7 - Javauser889030View Answer on Stackoverflow
Solution 8 - JavatoTemView Answer on Stackoverflow