美文网首页
Linux安装JDK完整步骤

Linux安装JDK完整步骤

作者: Pecksniff1994 | 来源:发表于2019-05-30 17:58 被阅读0次

    1、检查一下系统中的jdk版本

    [root@localhost software]# java -version
    

    显示:

    openjdk version "1.8.0_102"
    OpenJDK Runtime Environment (build 1.8.0_102-b14)
    OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)
    

    2、检测jdk安装包

    [root@localhost software]# rpm -qa | grep java
    

    显示:

    java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64
    python-javapackages-3.4.1-11.el7.noarch
    tzdata-java-2016g-2.el7.noarch
    javapackages-tools-3.4.1-11.el7.noarch
    java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64
    java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
    java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
    

    3、卸载openjdk

    [root@localhost software]# rpm -e --nodeps java-1.7.0-openjdk-1.7.0.111-2.6.7.8.el7.x86_64
    [root@localhost software]# rpm -e --nodeps java-1.7.0-openjdk-headless-1.7.0.111-2.6.7.8.el7.x86_64
    [root@localhost software]# rpm -e --nodeps java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64
    [root@localhost software]# rpm -e --nodeps java-1.8.0-openjdk-headless-1.8.0.102-4.b14.el7.x86_64
    

    或者使用

    [root@localhost jvm]# yum remove *openjdk*
    

    之后再次输入rpm -qa | grep java 查看卸载情况:

    [root@localhost software]# rpm -qa | grep java
    python-javapackages-3.4.1-11.el7.noarch
    javapackages-tools-3.4.1-11.el7.noarch
    

    4、安装新的jdk

    [root@localhost software]# ll
    total 181192
    -rw-r--r-- 1 root root 185540433 May 20  2017 jdk-8u131-linux-x64.tar.gz
    

    解压 jdk-8u131-linux-x64.tar.gz安装包

    [root@localhost software]# mkdir -p /opt/soft
    [root@localhost software]# mv jdk-8u131-linux-x64.tar.gz  /opt/soft
    [root@localhost software]# tar -zxvf jdk-8u131-linux-x64.tar.gz 
    [root@localhost software]# ln -s jdk1.8.0_131 jdk
    

    5、设置环境变量

    [root@localhost software]# vim /etc/profile
    

    在最前面添加:

    export JAVA_HOME=/opt/soft/jdk
    export JRE_HOME=${JAVA_HOME}/jre  
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  
    export  PATH=${JAVA_HOME}/bin:$PATH
    

    6、执行profile文件

    [root@localhost software]# source /etc/profile
    

    7、检查新安装的jdk

    [root@localhost software]# java -version
    

    显示:

    java version "1.8.0_131"
    Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
    

    到此为止,整个安装过程结束。

    8、脚本安装

    mkdir -p /opt/soft/
    cd  /opt/soft/
    
    #download jdk-8u161-linux-x64.tar.gz
    #wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u67-b01/jdk-7u67-linux-x64.tar.gz
    wget --no-check-certificate --no-cookies --header "Cookie: s_nr=1392900709523; ORA_WWW_PERSONALIZE=v:1~i:6~r:6~g:APAC~l:en~cs:NOT_FOUND~cn:scut; ORASSO_AUTH_HINT=v1.0~20140322121132; ORA_UCM_INFO=3~xxxx21212xxxx~xxxx~xxxx~xxxx@163.com; s_cc=true; oraclelicense=accept-securebackup-cookie; gpw_e24=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjava-archive-downloads-javase6-419409.html%23jdk-6u45-oth-JPR; s_sq=%5B%5BB%5D%5D;" http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.tar.gz
    
    #extract jdk
    #tar -xvf jdk-8u161-linux-x64.tar.gz
    tar -xvf jdk-8u161-linux-x64.tar.gz
    rm -f jdk
    ln -s jdk1.8.0_161 jdk
    
    
    #set environment
    export JAVA_HOME="/opt/soft/jdk"
    if ! grep "JAVA_HOME=/opt/soft/jdk" /etc/profile
    then
      echo "export JAVA_HOME=/opt/soft/jdk" | tee -a /etc/profile
      echo "export JRE_HOME=${JAVA_HOME}/jre " | tee -a /etc/profile
      echo "export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib" | tee -a /etc/environment
      echo "export  PATH=${JAVA_HOME}/bin:$PATH" | tee -a /etc/profile
    fi
    
    #update environment
    source /etc/profile
    echo "jdk is installed !"
    
    Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion' has value '1.8', bu...解决方案
    在cmd中运行命令java -version是出现错误:
    
    Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'
    
    has value '1.8', but '1.7' is required.
    Error: could not find java.dll
    Error: Could not find Java SE Runtime Environment.
    
    我的配置信息:我之前安装过jdk1.6和jdk1.7后来才安装jdk1.8,但是环境变量依旧指向jdk1.7 的,为什么显示jdk1.8?
    
    猜想:应该是jdk1.8安装后把jdk1.7覆盖了,也就是在注册表里的值把jdk1.7覆盖了
    
    解决过程:
    
    1.在环境变量Path中把%JAVA_HOME%\bin;移到最前,避免被后面的变量设置C:\ProgramData\Oracle\Java\javapath;覆盖
    
    2.在C:\Windows\System32 目录下搜索java,出来三个exe:java,javaw,javaws。把这个三个exe全删掉。
    
    (这三个exe就是jdk安装的时候被复制到系统目录C:\Windows\System32  下的)
    
    3.再在cmd中运行java -version,出现:
    
    java version "1.8.0_112"
    Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
    Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
    
    唉!这回好像正常了,但是还是不对,环境变量JAVA_HOME指向jdk1.7的 怎么出来的是jdk1.8?
    
    这进一步支持了前面的猜想:后来安装的1.8通过注册表的值把1,7替换掉了而不需要在环境变量JAVA_HOME
    
    设置就可以让系统认为jdk指向1.8。继续那就把指向1.8的删掉
    
    4.把环境变量Path下的C:\ProgramData\Oracle\Java\javapath;删掉,再在cmd中运行java -version,还是指向1.8,不对,继续...
    
    5.进入C:\ProgramData\Oracle\Java\javapath目录,把其下的java,javaw,javaws全删掉。(这就是jdk1.8生成的)。再次运行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)
    
    成功!
    
    原来就是C:\ProgramData\Oracle\Java\javapath这个目录的三个exe:java,javaw,javaws在作祟,把原来的jdk1.7的 环境变量指向都覆盖了,删了之后就系统无法定位到jdk1.8指向的java,就无法设置环境变量,然后jdk1.7的环境变量就起作用了。
    

    出处:https://www.jianshu.com/p/9b987388a518

    相关文章

      网友评论

          本文标题:Linux安装JDK完整步骤

          本文链接:https://www.haomeiwen.com/subject/wszvtctx.html