0

This reference explained how to include tools.jar in the dependencies. But I don't know where should I insert that code to? Should I insert it to the setting.xml of Maven or the pom.xml of my Java project?

I used the default maven in Eclipse 4.5.2(Mars.2 Release in Win7). I want to include tools.jar in my project. I could use the following code to include it in the pom.xml.

<dependency>
    <groupId>jdk.tools</groupId>
    <artifactId>jdk.tools</artifactId>
    <scope>system</scope>
    <version>1.4.2</version>
    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>

I also want to try to use java.home, not JAVA_HOME. I only know that they are different, but I don't exactly know the differences between them.

After reading that reference, I want to try it out. But I failed. So how should I use java.home to configure the pom.xml file to include tools.jar?

UPDATE:

I could reference java.home like this:

<project ...>

...

    <properties>
        <java.home>D:\Program Files\Java\jdk1.6.0_45</java.home>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>1.4.2</version>
            <scope>system</scope>
            <systemPath>${java.home}/lib/tools.jar</systemPath>
        </dependency>
    </dependencies>
</project>
niaomingjian
  • 3,472
  • 8
  • 43
  • 78
  • Note that Java 9 will not have tools.jar anymore: http://stackoverflow.com/questions/35240134/declare-maven-dependency-on-tools-jar-to-work-on-jdk-9 – ZhekaKozlov Feb 26 '17 at 15:48

2 Answers2

2

You can define a similar profile in your pom.xml as -

<!--This can help you use a custom java home path-->
<properties>
  <java.home>/your/path/to/java</java.home>
</properties>

...

<profiles>
  <profile>
    <id>default-tools.jar</id>
    <activation>
      <property>
        <name>java.vendor</name>
        <value>Sun Microsystems Inc.</value>
      </property>
    </activation>
    <dependencies>
      <dependency>
        <groupId>com.sun</groupId>
        <artifactId>tools</artifactId>
        <version>1.4.2</version>
        <scope>system</scope>
        <systemPath>${java.home}/relative/path/to/lib/tools.jar</systemPath>
      </dependency>
  </dependencies>
</profile>

Using the reference mentioned to form the profile above.

You need to specify the path to your custom Java home under the properties which can further replace it in the systemPath under the dependency where you specify further the relative path to the tools.jar.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • You mean I have to define `java.home` in the `properties` first, then I can use it in the `systemPath`. Otherwise, maven doesn't know what `java.home` is. – niaomingjian Feb 27 '17 at 00:51
  • No. I meant just in case you want to specify a custom path that is not already set in your environment variables or bash_profile. The properties is more like a variable whose value we are trying to replace in systemPath above. – Naman Feb 27 '17 at 00:58
  • I'm using Windows7. I have set JAVA_HOME in my environment variables. It's `D:\Program Files\Java\jdk1.8.0_66`. I could use it. But I just want to know how to use `java.home` in the profile without explicitly specifying the value of `java.home`. And I don't know the value of `java.home` in the profile.All I know is that in the java application the value of `System.getProperty("java.home")` is `D:\Program Files\Java\jdk1.6.0_45\jre`. I have two versions JDK installed in my machine. – niaomingjian Feb 27 '17 at 01:09
  • You can whichever you want to use by specifying its path under the `java.home` properties in pom.xml. *If not* specified maven would use the one which is set in the Windows environment variables instead. – Naman Feb 27 '17 at 03:45
0

@nullpointer is correct, just to add on the maven properties regarding your java home, you can see the output of the Maven java.home property as compared to your JAVA_HOME environment variable with running mvn validate and adding to your pom the code snippet below:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Displaying value of java home property: ${java.home}</echo>
                                <echo>Displaying value of JAVA_HOME variable: ${env.JAVA_HOME}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

For example, on my system it outputs (among other things):

 [echo] Displaying value of java home property: /usr/lib/jvm/java-8-openjdk-amd64/jre
 [echo] Displaying value of JAVA_HOME variable: /usr/lib/jvm/java-8-openjdk-amd64
Adonis
  • 4,670
  • 3
  • 37
  • 57
  • How many places where I can configure to affect the results of `java.home` and `JAVA_HOME` are there in my machine? – niaomingjian Feb 27 '17 at 01:23