博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How To Install Java on Ubuntu with Apt-Get
阅读量:4050 次
发布时间:2019-05-25

本文共 4842 字,大约阅读时间需要 16 分钟。

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:

sudo apt-get install default-jre

This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications (for example , , and  execute the following command:

sudo apt-get install default-jdk

That is everything that is needed to install Java.

All other steps are optional and must only be executed when needed.

Installing OpenJDK 7 (optional)

To install OpenJDK 7, execute the following command:

sudo apt-get install openjdk-7-jre

This will install the Java Runtime Environment (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-propertiessudo add-apt-repository ppa:webupd8team/javasudo 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  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:

sudo update-alternatives --config java

It will usually return something like this if you have 2 installations (if you have more, it will of course return more):

There are 2 choices for the alternative java (providing /usr/bin/java).Selection    Path                                            Priority   Status------------------------------------------------------------* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      auto mode  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      manual modePress enter to keep the current choice[*], or type selection number:

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

sudo update-alternatives --config 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: keytooljavadoc and jarsigner.

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:

sudo update-alternatives --config java

It returns something like:

There are 2 choices for the alternative java (providing /usr/bin/java).Selection    Path                                            Priority   Status------------------------------------------------------------* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      auto mode  1            /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java   1061      manual mode  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1062      manual modePress enter to keep the current choice[*], or type selection number:

The path of the installation is for each:

  1. /usr/lib/jvm/java-7-oracle

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

  3. /usr/lib/jvm/java-7-oracle

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

sudo nano /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.

转载地址:http://jfici.baihongyu.com/

你可能感兴趣的文章
需要统计的数据
查看>>
eclipse js jsp 卡怎么办,解决办法
查看>>
高性能J2EE接口平台设计
查看>>
spring mvc tomcat 线程池的坑
查看>>
JAVA 生成不重复订单号 优化版本 订单号格式为yyyymmdd后面自增
查看>>
mybatis-jpa插件使用教程
查看>>
cas 4.2.7 和 Nginx 整合遇到的问题 登录一会可以一会不可以
查看>>
CAS 单点登出 loginout 解决方案 -- 最靠谱的方案,不是抄的--还是不靠谱大家不要抄了
查看>>
springboot 支持jsp 直接访问jsp办法无需通过controller转发
查看>>
后续框架可以优化的点
查看>>
SpringBoot Logback 配置参数迁移到配置中心 Apollo
查看>>
springboot 自定义webroot的目录
查看>>
springboot springmvc j2ee 做伪静态化 .html 转发到 .jsp
查看>>
我入行以来java框架的几个阶段&论什么是真正的组件开发&项目中部分源码干货分享
查看>>
java-真正面向配置+组件的开发
查看>>
Mybattis Jpa插件简介
查看>>
java编程奇淫技巧之让java帮我们生成java代码后不停服,直接编译为class并且反射为对象直接使用
查看>>
springMVC源码阅读之FORM参数绑定
查看>>
springBoot SpringMVC解决XSS漏洞 -非重写request 支持普通form提交
查看>>
No plugin found for prefix 'install' in the current project解决方案
查看>>