Ubuntu 下JDK的安装与环境配置

时间:2021-09-20 14:36:06

在 Ubuntu 64 12.4 位操作系统下安装jdk-6u45-linux-x64.bin文件

一、复制JDK到安装目录

1.假设JDK安装文件放在桌面,我们要安装的目录为:/usr/local/java。
我们先在 /usr/local/目录下创建java文件夹:

aosp444@aosp444-virtual-machine:/usr/local$ sudo mkdir java
[sudo] password for aosp444:
aosp444@aosp444-virtual-machine:/usr/local$ ll
total 44
drwxr-xr-x 11 root root 4096 May 30 20:35 ./
drwxr-xr-x 10 root root 4096 Aug 7 2014 ../
drwxr-xr-x 2 root root 4096 Aug 7 2014 bin/
drwxr-xr-x 2 root root 4096 Aug 7 2014 etc/
drwxr-xr-x 2 root root 4096 Aug 7 2014 games/
drwxr-xr-x 2 root root 4096 Aug 7 2014 include/
drwxr-xr-x 2 root root 4096 May 30 20:35 java/
drwxr-xr-x 3 root root 4096 Aug 7 2014 lib/
lrwxrwxrwx 1 root root 9 May 30 19:38 man -> share/man/
drwxr-xr-x 2 root root 4096 Aug 7 2014 sbin/
drwxr-xr-x 7 root root 4096 Aug 8 2014 share/
drwxr-xr-x 2 root root 4096 Aug 7 2014 src/
aosp444@aosp444-virtual-machine:/usr/local$

2.切换到桌面,复制安卓文件到安装目录:

aosp444@aosp444-virtual-machine:~/Desktop$ sudo cp jdk-6u45-linux-x64.bin  /usr/local/java
aosp444@aosp444-virtual-machine:~/Desktop$

二、安装JDK

1.切换到JDK安装目录下,修改 安装文件权限:

aosp444@aosp444-virtual-machine:/usr/local/java$ sudo  chmod 777 jdk-6u45-linux-x64.bin
aosp444@aosp444-virtual-machine:/usr/local/java$

2.运行安装文件

aosp444@aosp444-virtual-machine:/usr/local/java$ sudo ./jdk-6u45-linux-x64.bin

3.安装成功后,java文件夹下多了一个文件夹:

aosp444@aosp444-virtual-machine:/usr/local/java$ ll
total 70412
drwxr-xr-x 3 root root 4096 May 30 20:46 ./
drwxr-xr-x 11 root root 4096 May 30 20:35 ../
drwxr-xr-x 8 root root 4096 Mar 27 2013 jdk1.6.0_45/
-rwxrwxrwx 1 root root 72087592 May 30 20:39 jdk-6u45-linux-x64.bin*
aosp444@aosp444-virtual-machine:/usr/local/java$

三、配置环境变量

1.打开/ect/profile 文件

sudo gedit /etc/profile

2.添加下面的环境变量,要根据安装目录修改,并保存。

# Java Environment
export JAVA_HOME=/usr/local/java/jdk1.6.0_45

export JRE_HOME=/usr/local/java/jdk1.6.0_45/jre

export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH

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

3.重启后使其生效并进行验证
也可使用下面的命令不重启使其生效,不过只针对当前域有效。

aosp444@aosp444-virtual-machine:/usr/local/java$ source /etc/profile
aosp444@aosp444-virtual-machine:/usr/local/java$

输入java -version 进行验证,成功后如下:

aosp444@aosp444-virtual-machine:/usr/local/java$ java -version
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
aosp444@aosp444-virtual-machine:/usr/local/java$