简体   繁体   中英

Set Java version on Linux Shell

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)

ps 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>

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM