Get the App
SLTechnology News&Howtos  ›  Database  › 

Switching multiple JDK environments under Ubuntu

Shulou Source: shulou.com Published: 2022-06-01 20:03:00 10月03日 Update

When you install Ubuntu, OpenJDK_1.7 is installed by default

$java-version

Java version "1.7.079"

OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.12.04.1)

OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

Now you need to install JDK_1.6

Installed in / opt/java/jdk1.6.0_45

/ opt/java/jdk1.6.0_45# ls

Bin db jre LICENSE README.html THIRDPARTYLICENSEREADME.txt

COPYRIGHT include lib man src.zip

Plus environmental variables

User environment variable: ~ / .add lines to bashrc

Export JAVA_HOME=/opt/java/jdk1.6.0_45

Export JRE_HOME=$JAVA_HOME/jre

Export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar

Export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

Export USE_CCACHE=1

System environment variable: add lines to / etc/profile

Export JAVA_HOME=/opt/java/jdk1.6.0_45

Export JRE_HOME=$JAVA_HOME/jre

Export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar

Export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

Export USE_CCACHE=1

Switch software versions through update-alternatives

Add command connectors for JDK_1.6, using the-- install option

$sudo update-alternatives-- install / usr/bin/java java/ opt/java/jdk1.6.0_45/bin/java 1062

To remove,-- remove option

$sudo update-alternatives-- remove / usr/bin/java java/ opt/java/jdk1.6.0_45/bin/java 1062

Create a default command link-- config

$sudo update-alternatives-config java

There are 2 choices for the alternative java (providing / usr/bin/java).

Selection Path Priority Status

* 0 / opt/java/jdk1.6.0_45/bin/java 1062 auto mode

1 / opt/java/jdk1.6.0_45/bin/java 1062 manual mode

2 / usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1051 manual mode

Press enter to keep the current choice [*], or type selection number:0

In the same way, Python can switch in this way.

2017-6-16 New

A more detailed English quote mark is as follows:

Use Oracle Java JDK, by following the steps below:

Visit http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase6-419409.html

Accept the license agreement

Download jdk-6uXX-linux-x64.bin (where XX is the latest minor version)

If you don't have an Oracle account and don't want to create one, you can have a look at http://bugmenot.com/view/oracle.com

Here is how to configure Oracle Java JDK to be the default on Ubuntu:

Cd / usr/lib/jvm & & sudo / bin/sh ~ / Downloads/jdk-6u38-linux-x64.bin-noregister

Note: You might get / bin/sh: jdk-6u43-linux-x64.bin: Permission denied error. This happens because root does not permissions to read the / Downloads folder.

Option 1: Temporarily change the permissions of ~ / Downloads folder for root to have read permission.

Chmod 755 ~ / Downloads/

Don't forget to change it back once you are done!

Option 2: Copy the downloaded file to / usr/lib/jvm and do

Sudo / bin/sh jdk-6u38-linux-x64.bin-noregister

In either case, then set it as default.

Sudo update-alternatives-install / usr/bin/javac javac / usr/lib/jvm/jdk1.6.0_38/bin/javac 50000sudo update-alternatives-install / usr/bin/java java / usr/lib/jvm/jdk1.6.0_38/bin/java 50000sudo update-alternatives-install / usr/bin/javaws javaws / usr/lib/jvm/jdk1.6.0_38/bin/javaws 50000sudo update-alternatives-install / usr/bin/javap javap / usr/lib/jvm/jdk1.6 0_38/bin/javap 50000sudo update-alternatives-- install / usr/bin/jar jar / usr/lib/jvm/jdk1.6.0_38/bin/jar 50000sudo update-alternatives-- install / usr/bin/jarsigner jarsigner / usr/lib/jvm/jdk1.6.0_38/bin/jarsigner 50000sudo update-alternatives-- config javacsudo update-alternatives-- config javasudo update-alternatives-- config javawssudo update-alternatives-- config javapsudo update-alternatives-- config jarsudo update-alternatives-- config jarsigner

Also make sure / usr/bin/java is first in your $PATH. I.e.

Which java

Should return / usr/bin/java.

Tags: Environment variables switches commands versions users systems software links English multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft MySQL MariaDB Xiaomi