ls -l /Library/Java/JavaVirtualMachines/
drwxr-xr-x 3 root wheel 96 Jun 15 14:30 jdk-11.0.7.jdk
drwxr-xr-x 3 root wheel 96 Jul 15 16:59 jdk1.8.0_261.jdk
# Tell maven to use explicit jdk
$ JAVA_HOME=`/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/` mvn -version
# Despite explicitly setting JAVA_HOME, mvn is still using JDK11
-bash: /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/: is a directory
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/Cellar/maven/3.6.3_1/libexec
Java version: 11.0.7, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-11.0.7.jdk/Contents/Home
I've also tried setting JAVA_HOME manually with:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
for some reason mvn insists on using jdk 11. Do you know where it might be overridden?
My pom.xml:
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${mvn-compiler.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<debug>true</debug>
<debuglevel>lines,vars,source</debuglevel>
</configuration>
</plugin>
</plugins>
Your JAVA_HOME exporting command is incorrect.
Your bash message also saying the same thing. You used back tick you need to use single quote or double quote
Correct statement would be
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home/ mvn --version
Related
Complementary to this question, how to determine actual java version used in mvn compile?
I am compiling FasterXML/jackson-databind as follows, but can't be sure of the java version:
$ git clone https://github.com/FasterXML/jackson-databind.git
$ cd jackson-databind
$ mvn compile
$ javap -verbose ./target/classes/com/fasterxml/jackson/databind/PropertyNamingStrategy.class | grep "major"
major version: 52
So according to this post, the java version is 1.8.
But when I grep the pom.xml I don't see that:
$ grep -wn "source" pom.xml
245: <id>add-test-source</id>
248: <goal>add-test-source</goal>
252: <source>src/test-jdk14/java</source>
265: <source>14</source>
I only see java 14 (?) What am I missing?
It's in the new release tag, not the source or compile -
You can reference it using the maven user property
NB Release overrides source and target values -
e.g. having source = 1.8, and target = 1.8, with release = 14,
compiles and releases in JDK 14
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<release>${maven.compiler.source}</release>
The parent POM is jackson-base which does set the java version : https://github.com/FasterXML/jackson-bom/blob/e60567a0e778f749d9e39e2c245486a44fbc52f9/base/pom.xml
<javac.src.version>1.8</javac.src.version>
<javac.target.version>1.8</javac.target.version>
<maven.compiler.source>${javac.src.version}</maven.compiler.source>
<maven.compiler.target>${javac.target.version}</maven.compiler.target>
You can inspect the fully evaluated POM with all the parents/dependencies/plugins/properties/etc using mvn help:effective-pom
The version that is used by maven can be found in POM file like:
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
or alternatively
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
I am trying to set the java version for a maven build when I execute it from a shell script. Fot some reason it is not picking up the intended java version. Any advise welcome.
I am running on a linux centos 7 os.
I have downloaded java 14 from https://jdk.java.net/14/ (Linux / x64), and extracted it to:
/usr/lib/jvm/jdk-14.0.2
pom.xml
<properties>
<java.version>14</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.release>${java.version}</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jsk.version>2.2.3</jsk.version>
<start-class>com.nexct.approvalservice.NexctApprovalServiceApplication</start-class>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<fork>true</fork>
</configuration>
</plugin>
deploy.sh
export JAVA_HOME=/usr/lib/jvm/jdk-14.0.2
export PATH=$JAVA_HOME/bin:$PATH
echo $JAVA_HOME
echo | java -version
echo "maven build ..."
mvn clean install -V -DskipTests -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2
output:
/usr/lib/jvm/jdk-14.0.2
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment (build 14.0.2+12-46)
OpenJDK 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
maven build ...
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/apache-maven-3.6.3
Java version: 1.7.0_161, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.161-2.6.12.0.el7_4.x86_64/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "3.10.0-957.21.3.el7.x86_64", arch: "amd64", family: "unix"
[INFO] BUILD FAILURE
Compilation failure
[ERROR] javac: invalid flag: -parameters
[ERROR] Usage: javac <options> <source files>
[ERROR] use -help for a list of possible options
I found a solution:
If I set, this it works. Maven compiles the code.
<executable>/usr/lib/jvm/jdk-14.0.2/bin/javac</executable>
However, this is not ideal because the path to the install is hard-coded, so on other servers with a different JAVA_HOME location, it won't work. Is it possible to reference $JAVA_HOME from within the POM file? and do something like this?
<executable><$JAVA_HOME>/bin/javac</executable>
e.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>${java.version}</release>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerVersion>${java.version}</compilerVersion>
<fork>true</fork>
<executable>/usr/lib/jvm/jdk-14.0.2/bin/javac</executable>
</configuration>
</plugin>
Everything seems correct, but it just won't work.
$ sudo mvn clean package
...
[INFO] Compilation failure
Failure executing javac, but could not parse the error:
javac: invalid target release: 1.7
Versions :
$ mvn -v
Apache Maven 2.2.1 (r801777; 2009-08-06 20:16:01+0100)
Java version: 1.7.0_75
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/jre
$ java -version
java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_75.jdk/Contents/Home/
pom.xml :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
For the record :
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
But I don't think this is the problem.
Any ideas ?
Try updating compiler plugin. Version 3.3 is the latest. If that does not help try using Maven 3+
You have to set the configuration for maven-compiler-plugin like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
Best is to configure this within a pluginManagement area.
mvn clean removes target folder which is read only.
Solution : Use sudo right ? Except that sudo no longer reads your variables or PATH but those of super user
sudo mvn -v now shows Java 1.6
Big thanks to khmarbaise
I am on the verge of releasing a project but it seem JAVA_HOME is been inconsistent. Maybe it's been overridden somewhere else? I am using ubuntu 14.04 and I have openjdk-7, java-7-oracle, java-8-oracle. Default java was set using update-java-alternatives
java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
mvn -version gives the following output
Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T17:37:52+00:00)
Maven home: /usr/share/maven3
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-39-generic", arch: "amd64", family: "unix"
But when doing mvn release:prepare, below is what I see:
[INFO] Not generating release POMs
[INFO] Executing goals 'clean verify'...
[WARNING] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[INFO] Error: JAVA_HOME is not defined correctly.
[INFO] We cannot execute /usr/lib/jvm/java-8-oracle/bin/java/bin/java
It looks like the java executor is being looked for in the wrong folder : /java/bin/java. I have tried changing java home to /usr/lib/jvm/java-8-orable/bin but it broke mvn-version check.
How to fix this? Thanks for reading
I can't find here to fix that. Has anyone encountered anything similar?
EDIT 1:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<excludes>
<exclude>**/*ITest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
<meminitial>128m</meminitial>
<maxmem>512m</maxmem>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<preparationGoals>clean verify</preparationGoals>
<tagBase>https://xxxx/svn/projectname/tags</tagBase>
</configuration>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9</version>
</plugin>
</plugins>
</pluginManagement>
</build>
I have switched to java-7-oracle and still no chance:
[INFO] Executing goals 'clean verify'...
[WARNING] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[INFO] Error: JAVA_HOME is not defined correctly.
[INFO] We cannot execute /usr/lib/jvm/java-7-oracle/bin/java/bin/java
EDIT 2:
Dear down voters, I am not asking for help about how to set Java Home. It's been set to
/usr/lib/jvm/java-8-oracle
Then to the following when I tried running same thing with java 7
/usr/lib/jvm/java-7-oracle
This is set in /etc/profile.d/jdk.sh by webup8 script
export J2SDKDIR=/usr/lib/jvm/java-7-oracle
export J2REDIR=/usr/lib/jvm/java-7-oracle/jre
export PATH=$PATH:/usr/lib/jvm/java-7-oracle/bin:/usr/lib/jvm/java-7-oracle/db/bin:/usr/lib/jvm/java-7-oracle/jre/bin
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export DERBY_HOME=/usr/lib/jvm/java-7-oracle/db
EDIT 3
I have switched to openjdk 7 , edited the jdk.sh to reflect this export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-amd64 and the error went. I run to other famous issues (permission issues to tags folder. weird) . So it's not related to the maven itself I guess. But this is weird that it works fine for openjdk ...
JAVA_HOME must point to jre, not jdk. Then set :
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
Creating a file .mavenrc on my home folder and adding the code below solved the problem for me. (Ubuntu 14.10, Maven 3.2.1)
export JAVA_HOME=/usr/lib/jvm/java-8-oracle
Another workaround:
export JAVACMD=$JAVA_HOME/bin/java
setting /usr/lib/jvm/java ⇒ /usr/java/jdk1.7.0_45 did not work for me
I spend quite some time to tackle similar error. It looks like package manager might create mess in Java installations. Path to Java is sometimes hardcoded in bash files. I found one in /etc/profile.d/jdk.sh. It just overrides your settings. Another fix/workaround for that is to update symlink, in my case it was /usr/lib/jvm/java ⇒ /usr/java/jdk1.7.0_45. Try to look for java/bin text in all bash files.
in super user privilege on your terminal open
vi etc/environment
on opened file add JAVA_HOME path
JAVA_HOME="/usr/lib/jvm/java-7-openjdk-i386/"
Hope you set the JAVA_HOME
check if your JAVA_HOME is set using command
echo $JAVA_HOME
I used OpenJDK as default java , updated JAVA_HOME in /etc/environment and all the issues went. Thanks for all your effort helping me through this
I had the same problem. My workaround was:
cd /usr/lib/jvm/java-8-oracle/jre/bin
sudo ln -s /usr/lib/jvm/java-8-oracle/bin/java java
I have setup maven in my terminal, and when getting the version settings (using mvn -v) it seems it uses JDK 1.6, while I have JDK 1.7 installed. Is there anything wrong?
The commands I enter are these:
blues:helloworld Ninja$ java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)`
blues:helloworld Ninja$ mvn -v
Apache Maven 3.1.0 (893ca28a1da9d5f51ac03827af98bb730128f9f2; 2013-06-28 10:15:32+0800)
Maven home: /usr/local/Cellar/maven/3.1.0/libexec
Java version: 1.6.0_51, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: zh_CN, platform encoding: EUC_CN
OS name: "mac os x", version: "10.8.4", arch: "x86_64", family: "mac"
add the following to your ~/.mavenrc:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/{jdk-version}/Contents/Home
Second Solution:
echo export "JAVA_HOME=\$(/usr/libexec/java_home)" >> ~/.bash_profile
Get into
/System/Library/Frameworks/JavaVM.framework/Versions
and update the CurrentJDK symbolic link to point to
/Library/Java/JavaVirtualMachines/YOUR_JDK_VERSION/Contents/
E.g.
cd /System/Library/Frameworks/JavaVM.framework/Versions
sudo rm CurrentJDK
sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/ CurrentJDK
Now it shall work immediately.
You can also do,
<properties>
...
<!-- maven-compiler-plugin , aka JAVA Compiler 1.7 -->
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
...
</properties>
You can also explicitly tell maven which java version to compile for. You can try adding the maven-compiler-plugin to your pom.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
If you imported a maven project into an IDE, then there is probably a maven setting in your IDE for default compiler that your maven runner is using.
It helped me. Just add it in your pom.xml.
By default maven compiler plugin uses Java 1.5 or 1.6, you have to redefine it in your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
For Eclipse Users.
If you have a Run Configuration that does clean package for example.
In the Run Configuration panel there is a JRE tab where you can specify against which runtime it should run.
Note that this configuration overrides whatever is in the pom.xml.
I am late to this question, but I think the best way to handle JDK versions on MacOS is by using the script described at: http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/
Please check the compatibility. I struggled with mvn 3.2.1 and jdk 1.6.0_37 for many hours. All variables were set but was not working. Finally I upgraded jdk to 1.8.0_60 and mvn 3.3.3 and that worked. Environment Variables as following:
JAVA_HOME=C:\ProgramFiles\Java\jdk1.8.0_60
MVN_HOME=C:\ProgramFiles\apache-maven\apache-maven-3.3.3
M2=%MVN_HOME%\bin extend system level Path- ;%M2%;%JAVA_HOME%\bin;
#MasterGaurav's solution works perfectly.
I normally put the Java switch statement into a zsh function:
alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'
function java_use() {
export JAVA_HOME=$(/usr/libexec/java_home -v $1)
echo export "JAVA_HOME=$(/usr/libexec/java_home -v $1)" > ~/.mavenrc
export PATH=$JAVA_HOME/bin:$PATH
java -version
}
You can run java_ls to get all of the available JVMs on your machine,
and then java_use 1.7 to use 1.7 for both Java and Maven.