0

I am running the following script on Linux Centos 7.

export JAVA_HOME=/usr/lib/jvm/jdk-14.0.2
echo $JAVA_HOME
echo | java -version
echo "maven build ..."
mvn clean install -DskipTests -Dhttps.protocols=TLSv1,TLSv1.1,TLSv1.2

With this output:

/usr/lib/jvm/jdk-14.0.2
java version "1.7.0_161"
OpenJDK Runtime Environment (rhel-2.6.12.0.el7_4-x86_64 u161-b00)
OpenJDK 64-Bit Server VM (build 24.161-b00, mixed mode)
maven build ...

As you can see, the java version is 1.7.0_161.

Question

How do I set the java version to java 14, so that maven builds with java 14?

More info:

The java 14 version I installed and I set the JAVA_HOME on is from here: https://jdk.java.net/14/ (Linux / x64)

p.s. I only want to use java 14 for this maven build. I still want to keep Java 1.7 globally.

UPDATE

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>
Richard
  • 8,193
  • 28
  • 107
  • 228
  • Add -V to maven command line to see which version of Java is used. – Robert Hume Jul 22 '20 at 14:47
  • Hi Robert, thanks for the reply. After adding -V, I get: `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"` – Richard Jul 22 '20 at 14:49
  • Did you check [this](https://stackoverflow.com/questions/2165061/java-version-still-showing-as-1-4-linux) out? – Amit Kumar Jul 22 '20 at 14:54
  • @AmitKumar, thanks. I am trying to figure `update-alternatives` out. – Richard Jul 22 '20 at 15:06
  • I find this a bit confusing. Your title and tags mention Linux and shell, but you really seem to be concerned about a Maven build? Can't you just [specify the Java version in the build script](https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html)? – Robert Jul 22 '20 at 17:42
  • Hi Robert, that's the thing, I do specify the java version in my POM file, but it doesn't seem to work. So on my dev environment I set the JAVA_HOME, and that works. Am I doing something wrong when trying to set java 14 in the pom? (see update above in the original question). – Richard Jul 23 '20 at 06:15
  • I have asked a specific question with your suggested updates here: https://stackoverflow.com/questions/63048137/setting-java-version-for-maven-build?noredirect=1#comment111491889_63048137 – Richard Jul 23 '20 at 06:36

2 Answers2

0

Search for the location where maven is installed.

In windows when you unzip the apache-maven.zip it contains a bin directory.

Path looks like this in my system. E:\builds\apache-maven-3.5.0\bin\mvn

You need to check what is the value for JAVA_HOME is set in mvn file.

Santosh b
  • 729
  • 1
  • 8
  • 19
  • I don't understand? – Richard Jul 22 '20 at 15:45
  • 2
    That sounds terribly complicated; why would I modify installed packages to specify the Java version to be used for compiling *my* code? The next system upgrade will also overwrite your change. – Robert Jul 22 '20 at 17:38
0

You need to set more than JAVA_HOME. Do something like:

export JAVA_HOME=/usr/lib/jvm/jdk-14.0.2
export PATH=$JAVA_HOME/bin:$PATH

This changes the JAVA_HOME to your Java 14 install but then also changes where the shell finds things like javac and so on.

I create a script like the above named, in this case java14, and then can (assuming bash shell) do a source /home/me/bin/java14 (I store it in my local bin directory) to change it when I need.

I find this easier than update-alternatives. I often want to make a change for a single window, not the entire machine.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • thank you for your answer. I like your approach, because as you say it is simpler the `update-alternatives`. However, when I try it in my `.sh` file, it doesn't change the java version. Am I doing something wrong? I do: `export JAVA_HOME=/usr/lib/jvm/jdk-14.0.2 export PATH=$JAVA_HOME/bin:$PATH`, but get: `Java version: 1.7.0_161`. – Richard Jul 23 '20 at 06:09
  • Setting the `JAVA_HOME` works for me on a MacOS, but on Linux Centos7 I cannot seem to get it to work. – Richard Jul 23 '20 at 06:10
  • You need to `source` the file, not run it. When you do the `source` command it's the same as typing it. If you run it as a shell it's set during the running of that shell and then it's not. So create your file and then run `source /path/to/my/file.sh` on both MacOS and CentOS and it should work. – stdunbar Jul 23 '20 at 14:11