Introduction
As a lot of articles and programs require to have Java installed, this article will guide you through the process of installing and managing different versions of Java.Installing default JRE/JDK
This is the recommended and easiest option. This will install OpenJDK 6 on Ubuntu 12.04 and earlier and on 12.10+ it will install OpenJDK 7.Installing Java with apt-get is easy. First, update the package index:
- sudo apt-get update
- java -version
- sudo apt-get install default-jre
If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications (for example Apache Ant, Apache Maven, Eclipse and IntelliJ IDEA execute the following command below, That is everything that is needed to install Java.
- sudo apt-get install default-jdk
All other steps are optional and must only be executed when needed.
Installing OpenJDK 7 (optional)
To install OpenJDK 7, execute the following command below, this will install the Java Runtime Environment (JRE).- sudo apt-get install openjdk-7-jre
- sudo apt-get install openjdk-7-jdk
The Oracle JDK is the official JDK; however, it is no longer provided by Oracle as a default installation for Ubuntu.
You can still install it using apt-get. To install any version, first execute the following commands:
- sudo apt-get install python-software-properties
- sudo add-apt-repository ppa:webupd8team/java
- sudo apt-get update
Oracle JDK 6
This is an old version but still in use.- sudo apt-get install oracle-java6-installer
Oracle JDK 7
This is the latest stable version.- sudo apt-get install oracle-java7-installer
Oracle JDK 8
This is a developer preview, the general release is scheduled for March 2014. This external article about Java 8 may help you to understand what it's all about.- sudo apt-get install oracle-java8-installer
Managing Java (optional)
When there are multiple Java installations on your Droplet, the Java version to use as default can be chosen. To do this, execute the following command:It will usually return something like this if you have 2 installations (if you have more, it will of course return more):
- root@stb-she-003:~# update-alternatives --config java
- There are 3 choices for the alternative java (providing /usr/bin/java).
- Selection Path Priority Status
- ------------------------------------------------------------
- 0 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 auto mode
- 1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
- * 2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
- 3 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1051 manual mode
- Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode.
It is the same selection screen as the previous command and should be used in the same way. This command can be executed for all other commands which have different installations. In Java, this includes but is not limited to: keytool, javadoc and jarsigner.
- root@stb-she-003:~# javac -version
- javac 1.6.0_26
- root@stb-she-003:~#
- root@stb-she-003:~# update-alternatives --config javac
- There are 2 choices for the alternative javac (providing /usr/bin/javac).
- Selection Path Priority Status
- ------------------------------------------------------------
- 0 /usr/lib/jvm/java-6-openjdk-amd64/bin/javac 1061 auto mode
- 1 /usr/lib/jvm/java-6-openjdk-amd64/bin/javac 1061 manual mode
- * 2 /usr/lib/jvm/java-6-sun/bin/javac 63 manual mode
- Press enter to keep the current choice[*], or type selection number: 2
- root@stb-she-003:~# apt-get install openjdk-7-jdk
- Reading package lists... Done
- Building dependency tree
- Reading state information... Done
- Suggested packages:
- openjdk-7-demo openjdk-7-source visualvm
- The following NEW packages will be installed:
- openjdk-7-jdk
- 0 upgraded, 1 newly installed, 0 to remove and 1 not upgraded.
- Need to get 16.6 MB of archives.
- After this operation, 20.7 MB of additional disk space will be used.
- Get:1 http://ks-ubuntu-repo.broadcom.com/ubuntu/repo/precise/ precise-updates/universe openjdk-7-jdk amd64 7u7-2.3.2a-0ubuntu0.12.04.1 [16.6 MB]
- Fetched 16.6 MB in 14s (1,115 kB/s)
- Selecting previously unselected package openjdk-7-jdk.
- (Reading database ... 252394 files and directories currently installed.)
- Unpacking openjdk-7-jdk (from .../openjdk-7-jdk_7u7-2.3.2a-0ubuntu0.12.04.1_amd64.deb) ...
- Setting up openjdk-7-jdk (7u7-2.3.2a-0ubuntu0.12.04.1) ...
- /usr/lib/jvm/java-6-sun/bin/apt
- update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/bin/jcmd to provide /usr/bin/jcmd (jcmd) in auto mode.
- root@stb-she-003:~# update-alternatives --config javac
- There are 3 choices for the alternative javac (providing /usr/bin/javac).
- Selection Path Priority Status
- ------------------------------------------------------------
- 0 /usr/lib/jvm/java-6-openjdk-amd64/bin/javac 1061 auto mode
- 1 /usr/lib/jvm/java-6-openjdk-amd64/bin/javac 1061 manual mode
- * 2 /usr/lib/jvm/java-6-sun/bin/javac 63 manual mode
- 3 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1051 manual mode
- Press enter to keep the current choice[*], or type selection number: 3
- update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/bin/javac to provide /usr/bin/javac (javac) in manual mode.
Setting the "JAVA_HOME" environment variable
To set the JAVA_HOME environment variable, which is needed for some programs, first find out the path of your Java installation:- root@stb-she-003:~# update-alternatives --config java
- There are 3 choices for the alternative java (providing /usr/bin/java).
- Selection Path Priority Status
- ------------------------------------------------------------
- 0 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 auto mode
- 1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
- * 2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
- 3 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1051 manual mode
- Press enter to keep the current choice[*], or type selection number:
- /usr/lib/jvm/java-6-openjdk-amd64
- /usr/lib/jvm/java-6-sun/
- /usr/lib/jvm/java-7-openjdk-amd64
- sudo vim /etc/environment
- JAVA_HOME="YOUR_PATH"
- source /etc/environment
- echo $JAVA_HOME
Specially, on my server do follow:
- apt-get update
- apt-get install git gnupg flex bison gperf build-essential zip curl libc6-dev libncurses5-dev:i386 x11proto-core-dev libx11-dev:i386 libreadline6-dev:i386 libgl1-mesa-glx:i386 libgl1-mesa-dev g++-multilib mingw32 tofrodos python-markdown libxml2-utils xsltproc zlib1g-dev:i386
- apt-get install default-jre default-jdk openjdk-7-jre openjdk-7-jdk
- update-alternatives --config java
- update-alternatives --config javac
Updated, install JDK8 on Ubuntu 14/12
点击(此处)折叠或打开
- 1. add-apt-repository ppa:openjdk-r/ppa
- 2. apt-get update
- 3. apt-get install openjdk-8-jdk
- 4. update-alternatives --config java
- 5. java -version
- root@bld-bei-016:~# update-alternatives --config java
- There are 4 choices for the alternative java (providing /usr/bin/java).
- Selection Path Priority Status
- ------------------------------------------------------------
- 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1069 auto mode
- 1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
- 2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
- * 3 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1051 manual mode
- 4 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1069 manual mode
- Press enter to keep the current choice[*], or type selection number: 4
- update-alternatives: using /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode.
- root@bld-bei-016:~# java -version
- openjdk version "1.8.0_111"
- OpenJDK Runtime Environment (build 1.8.0_111-8u111-b14-3~12.04-b14)
- OpenJDK 64-Bit Server VM (build 25.111-b14, mixed mode)