java verbei_Install Java with apt-get and Switch Java Version on Ubuntu

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

Then, check if Java is not already installed:

java -version

If it returns "The program java can be found in the following packages", Java hasn't been installed yet, so execute the following command below, This will install the Java Runtime Environment (JRE).

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

If you instead need the Java Development Kit (JDK), execute the following command:

sudo apt-get install openjdk-7-jdk

Installing Oracle JDK (optional)

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

Then, depending on the version you want to install, execute one of the following commands:

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.

You can now choose the number to use as default. This can also be done for the Java compiler (javac):

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

If need to use javac, then we need to install openjdk-7-jdk below, then select javac 1.7 below:

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:

The path of the installation is for each:

/usr/lib/jvm/java-6-openjdk-amd64

/usr/lib/jvm/java-6-sun/

/usr/lib/jvm/java-7-openjdk-amd64

Copy the path from your preferred installation and then edit the file /etc/environment:

sudo vim /etc/environment

In this file, add the following line (replacing YOUR_PATH by the just copied path):

JAVA_HOME="YOUR_PATH"

That should be enough to set the environment variable. Now reload this file:

source /etc/environment

Test it by executing:

echo $JAVA_HOME

If it returns the just set path, the environment variable has been set successfully. If it doesn't, please make sure you followed all steps correctly.

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值