目录
1、下载JDK
2、卸载JDK
3、安装JDK
3.1、.rpm后缀格式JDK安装方式
3.2、.tar.gz后缀格式JDK安装方式
4、验证安装
1、下载JDK
根据操作系统的位数下载对应的JDK,操作系统是32位的就下32位的JDK,64位的就下64位的JDK。Linux x86是32位JDK,Linux x64是64位JDK。
Linux下的JDK安装包现有两种格式.rpm和.tar.gz。rpm是redhat红帽的标准安装包,rpm安装时会自动配置。
本安装示例在CentOS 6.6系统下进行,其它类型Linux系统安装方式相同,下载的JDK版本为1.7.0_79,对.rpm和.tar.gz安装方式分别说明,本处已下载两种格式的文件。
jdk-7u79-linux-x64.rpm
jdk-7u79-linux-x64.tar.gz
下载地址:http://www.oracle.com/technetwork/Java/javase/downloads/jdk7-downloads-1880260.html
2、卸载JDK
查看系统是否已安装JDK。一般的linux都默认使用了开源的openJDK。显示JDK版本信息,已经安装JDK,否则没有安装。命令行:
[plain] view plain copy
- java -version
[[email protected] Desktop]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
查找名字包含java,jdk的已安装程序。查找到了,已经安装JDK,否则没有安装。命令行:
[plain] view plain copy
- rpm -qa | grep java
- rpm -qa | grep jdk
[[email protected] Desktop]# rpm -qa | grep java
[[email protected] Desktop]# rpm -qa | grep jdk
jdk-1.7.0_79-fcs.x86_64
已安装JDK,卸载系统上的JDK。
单个卸载程序,使用rpm -e xxx命令。命令行:
[plain] view plain copy
- rpm -e jdk-1.7.0_79-fcs.x86_64
[[email protected] Desktop]# rpm -e jdk-1.7.0_79-fcs.x86_64
批量卸载所有名字包含jdk的已安装程序。命令行:
[plain] view plain copy
- rpm -qa | grep jdk | xargs rpm -e --nodeps
[[email protected] Desktop]# rpm -qa | grep jdk | xargs rpm -e --nodeps
批量卸载所有名字包含java的已安装程序。命令行:
[plain] view plain copy
- rpm -qa | grep java | xargs rpm -e --nodeps
[[email protected] Desktop]# rpm -qa | grep java | xargs rpm -e --nodeps
卸载后,查看JDK版本,已无信息,卸载成功。命令行:
[plain] view plain copy
- java -version
[[email protected] Desktop]# java -version
bash: /usr/bin/java: No such file or directory
3、安装JDK
3.1、.rpm后缀格式JDK安装方式
安装jdk-7u79-linux-x64.rpm。
给安装包添加权限。命令行:
[plain] view plain copy
- chmod 755 jdk-7u79-linux-x64.rpm
[[email protected] Desktop]# chmod 755 jdk-7u79-linux-x64.rpm
使用rpm -ivh xxx命令安装JDK。命令行:
[plain] view plain copy
- rpm -ivh jdk-7u79-linux-x64.rpm
[[email protected] Desktop]# rpm -ivh jdk-7u79-linux-x64.rpm
Preparing... ########################################### [100%]
1:jdk ########################################### [100%]
Unpacking JAR files...
rt.jar...
jsse.jar...
charsets.jar...
tools.jar...
localedata.jar...
jfxrt.jar...
安装.rpm格式后缀的JDK不需要配置java环境变量,会自动配置,会在/usr/bin生成java、javac等JDK的快捷方式图标,这些图标会链接指向到/usr/java/jdk1.7.0_79中。用echo $PATH查看环境变量,可以看到/usr/bin是加入了PATH环境变量中的。命令行:
[plain] view plain copy
- echo $PATH
[[email protected] Desktop]# echo $PATH
/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/root/bin
3.2、.tar.gz后缀格式JDK安装方式
安装jdk-7u79-linux-x64.tar.gz。
在/usr目录下新建java文件夹,命令行:
[plain] view plain copy
- mkdir /usr/java
[[email protected] Desktop]# mkdir /usr/java
进入JDK压缩包所在目录,将压缩包复制到java文件夹中。复制文件用cp xxx命令,复制文件夹用cp -r xxx,命令行:
[plain] view plain copy
- cp jdk-7u79-linux-x64.gz /usr/java
[[email protected] Desktop]# cp jdk-7u79-linux-x64.gz /usr/java
然后返回到根目录,再进入java目录,命令行:
[plain] view plain copy
- cd /
- cd /usr/java
[[email protected] Desktop]# cd /
[[email protected] Desktop]# cd /usr/java
压缩包解压,命令行:
[plain] view plain copy
- tar xvf jdk-7u79-linux-x64.gz
[[email protected] Desktop]# tar xvf jdk-7u79-linux-x64.gz
删除压缩包,输入yes确认删除。命令行:
[plain] view plain copy
- rm jdk-7u79-linux-x64.gz
[[email protected] java]# rm jdk-7u79-linux-x64.gz
rm: remove regular file `jdk-7u79-linux-x64.gz‘? yes
修改/etc/profile文件设置系统变量,设置jdk环境变量,该方式对所有用户有效。
使用VI编辑,输入命令,回车确认。命令行:
[plain] view plain copy
- vi /etc/profile
[[email protected] Desktop]# vi /etc/profile
打开之后在文件末尾添加下面配置。通过鼠标滑轮滚动到文件末尾,上下左右方向键控制光标输入位置。
[plain] view plain copy
- export JAVA_HOME=/usr/java/jdk1.7.0_79
- export JRE_HOME=$JAVA_HOME/jre
- export PATH=$PATH:$JAVA_HOME/bin
- export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
配置好后,按Esc退出,按Shift加英文冒号shift + : 然后输入wq,按Enter回车键确认。取消编辑按Ctrl+z。
使profile配置生效。命令行:
[plain] view plain copy
- source /etc/profile
[[email protected] Desktop]# source /etc/profile
修改.bashrc文件设置用户变量,设置jdk环境变量,该方式仅对当前用户有效,对其它用户无效,其它用户没有运行java命令的权限。命令行:
[plain] view plain copy
- vi ~/.bashrc
[[email protected] Desktop]# vi ~/.bashrc
在bashrc文件中添加下面配置。按Insert键进入编辑状态。
[plain] view plain copy
- export JAVA_HOME=/usr/java/jdk1.7.0_79
- export JRE_HOME=$JAVA_HOME/jre
- export PATH=$PATH:$JAVA_HOME/bin
- export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
使bashrc配置生效。命令行:
[plain] view plain copy
- source ~/.bashrc
[[email protected] Desktop]# source ~/.bashrc
配置好后,按Esc退出,按Shift加英文冒号shift + : 然后输入wq,按Enter回车键确认。取消编辑按Ctrl+z。
如果要使JDK对某个用户有效,某个用户具有运行java命令的权限,修改该用户的.bashrc文件。
例如:ROOT用户给dodobook2用户名运行java命令的权限。
[plain] view plain copy
- vi /home/dodobook2/.bashrc
[[email protected] root]# vi /home/dodobook2/.bashrc
在bashrc文件中添加下面配置。按Insert键进入编辑状态。
[plain] view plain copy
- export JAVA_HOME=/usr/java/jdk1.7.0_79
- export JRE_HOME=$JAVA_HOME/jre
- export PATH=$PATH:$JAVA_HOME/bin
- export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
如果当前登录用户是dodobook2,使用vi ~/.bashrc命令编辑修改。
[plain] view plain copy
- vi ~/.bashrc
[[email protected] Desktop]# vi ~/.bashrc
使用echo $JAVA_HOME、echo $CLASSPATH、echo $PATH查看环境变量配置。
[plain] view plain copy
- echo $JAVA_HOME
- echo $CLASSPATH
- echo $PATH
[[email protected] Desktop]# echo $JAVA_HOME
/usr/java/jdk1.7.0_79
[[email protected] Desktop]# echo $CLASSPATH
./:/usr/java/jdk1.7.0_79/lib:/usr/java/jdk1.7.0_79/jre/lib
[[email protected] Desktop]# echo $PATH
/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/java/jdk1.7.0_79/bin:/root/bin:/usr/java/jdk1.7.0_79/bin
4、验证安装
安装完成后,查看JDK版本。命令行:
[plain] view plain copy
- java -version
[[email protected] Desktop]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
测试下javac命令是否可用。命令行:
[plain] view plain copy
- javac
[[email protected] Desktop]# javac
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-Werror Terminate compilation if warnings occur
@<filename> Read options and filenames from file
还可以写段代码测试下。用VI编辑java代码测试JDK是否能正常编译。按Insert键进入编辑状态。命令行:
[plain] view plain copy
- vi test.java
[plain] view plain copy
- class test
- {
- public static void main(String[]args)
- {
- System.out.println("Hello World!");
- }
- }
[[email protected] Desktop]# vi test.java
class test
{
public static void main(String[]args)
{
System.out.println("Hello World!");
}
}
写好后,按Esc退出,按Shift加英文冒号shift + : 然后输入wq,按Enter回车键确认。
编译运行。命令行:
[plain] view plain copy
- javac test.java
[plain] view plain copy
- java test
[[email protected] Desktop]# javac test.java
[[email protected] Desktop]# java test
Hello World!