When i try to update maven project I get the error that maven cant find tools.jar in JRE locations shown below:
However I have installed and specefied JDK in Windows->Preferences->Java->Installed JRE.
Is there any other place the JDK should be specified in order to resolved this tools.jar issues.
Note: tools.jar is located in lib of JDK_HOME directory and i also specified the jar as mentioned in this answer but it did not resolve issue.
Go to pom.xml and then:
<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}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
Related
I have an application with a normal main method, running as a normal jar within my IDE. It's a spring-boot application that runs on an embedded tomcat by default.
To package it as war file with maven is as simple as:
<packaging>war</packaging>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Problem: I'd still would like to run the app locally in my IDE embedded. Thus exclude the dependency above, and leave packaging to >jar<.
Question: is it possible to eg define profiles in maven, so that by default the packaging is =jar, but when running a specific profile it is exchanged with war and the tomcat server dependency is included?
Ok, it's as simple as:
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>jar</packaging.type>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
+for IntelliJ IDEA: running the appropriate maven command is as simple as: checking the desired profile an click on 'package'.
I have never built my java applications by maven. But when i am trying to do that it's giving me error.
I have created JAR file from other java application just by exporting as JAR from that application.
Now i want to add this JAR in my maven application. I don't really how to do that.
this is how i have added in pom.xml. But i don't really know what should be it's artifact id.
Seriously what is artifact id?
<dependency>
<groupId>ProjectZen</groupId>
<artifactId>community</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}\libs\ProjectZen.jar</systemPath>
</dependency>
I am getting below error
Missing artifact ProjectZen:community:jar:1
Thanks
Fahad Mullaji
If it is custom jar you need to do following things
Open cmd and type following command
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=ProjectZen
-DartifactId=community
-Dversion=1
-Dpackaging=jar
-DgeneratePom=true
Now, the “ProjectZen” jar is copied to your Maven local repository.
In pom.xml
<dependency>
<groupId>ProjectZen</groupId>
<artifactId>community</artifactId>
<scope>system</scope>
<version>1</version>
<systemPath>${basedir}\libs\ProjectZen.jar</systemPath>
</dependency>
now the “ProjectZen” jar is able to retrieve from your Maven local repository.
change
<systemPath>${basedir}\libs\ProjectZen.jar</systemPath>
to
<systemPath>${basedir}/libs/ProjectZen.jar</systemPath>
or install it in local maven cache
you should give the format as below. and the slashes used in are incorrect I suppose. Check the dependency in this format.
...
<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}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
Reference
...
This question already has answers here:
JDK tools.jar as maven dependency
(8 answers)
Closed 8 years ago.
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:
JDK tools.jar as maven dependency
Build error: missing artifact com.sun:tools:jar:1.6
missing artifact sun.jdk:tools:jar:1.6.0:system
Missing artifact com.sun:tools:jar:1.5.0 maven
I want to checkout sonar, so I added the following snippet to my pom.xml the dependency part was taken from http://maven.apache.org/general.html#tools-jar-dependency
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:derby://localhost:1527/sonar;create=true</sonar.jdbc.url>
<sonar.jdbc.driverClassName>org.apache.derby.jdbc.ClientDriver
</sonar.jdbc.driverClassName>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.host.url>http://localhost:8080/sonar</sonar.host.url>
</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>
</profile>
Unfortunatly the error persists
Embedded error: Missing:
----------
1) com.sun:tools:jar:1.4.2
Try downloading the file manually from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=/path/to/file
I also followed the suggestion to add the missing jar manually to the repository, which had no effect.
mvn install:install-file -DgroupId=com.sun -DartifactId=tools -Dversion=1.4.2 -Dpackaging=jar -Dfile=$JAVA_HOME/lib/tools.jar
What am I doing wrong?
EDIT:
I verified that tools.jar has been added to my local repository. In debug mode maven shows the error:
1 required artifact is missing.
for artifact:
group:artifact:war:1.0.0-BUILD-SNAPSHOT
from the specified remote repositories:
central (http://repo1.maven.org/maven2)
Are you running this in eclipse? If the answer is yes, this is an annoying and very misunderstood problem. Take a look at my answer here
You may not be pointing eclipse to the right jre/jdk when you're starting up (this is something you didn't necessarily configure rather was Windows)
A problem I had once was different location of tools.jar under Mac OS. Here's the profiles section to solve the problem:
<profiles>
<profile>
<id>java-home-parent-lib-tools-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
<profile>
<id>java-home-parent-classes-classes-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<dependencies>
<dependency>
<groupId>sun.jdk</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../Classes/classes.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
However I am not sure this is something you're facing.
If your JAVA_HOME points to your jdk (e.g./usr/lib/jvm/jdk1.6.0_33), your tools.jar configuration (${java.home}/../lib/tools.jar) indicates that there should be a tools jar with the following path: /usr/lib/jvm/lib/tools.jar.
If you change the tools jar path to ${java.home}/lib/tools.jar and verify that in your JAVA_HOME/lib there is the tools.jar file, it should work.
There you can find the related jira.
From Maven's website:
...
<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}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
...
tools.jar is already inclduded on Macs, embedded with classes.jar. Was there no way to specify !mac in the activation settings (except for listing every os except the mac) in the pom.xml, instead of always getting:
ERROR>warning: [path] bad path element ""/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/lib/tools.jar"": no such file or directory
You can use the '!' modifier in the os section of activation (tried and works with Maven 3.0.3). Uses the same algorithm as the enforcer plugin, described here.
<profiles>
<profile>
<id>not-mac</id>
<activation>
<os>
<family>!mac</family>
</os>
</activation>
<properties>
<!-- Define non-mac properties here -->
</properties>
</profile>
</profiles>
See this article for a more complete description of using tools.jar in Maven.
I found another method. Have the dependency in an "activeByDefault" profile. Have another profile (profile-2) which gets activated if the OS is mac. According to this the "activeByDefault" profiles get deactivated if any of the other profiles get activated. So if the OS is mac, profile-2 will get activated, which will deactivate the "activeByDefault" profile.