系统版本

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

#安装之前先查看一下有无系统自带jdk

rpm -qa |grep java

rpm -qa |grep jdk

rpm -qa |grep gcj

#如果有就使用批量卸载命令

rpm -qa | grep java | xargs rpm -e --nodeps 

 

直接yum安装1.8.0版本openjdk

[root@localhost ~]# yum install java-1.8.0-openjdk* -y

查看版本

[root@localhost ~]# java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)

默认jre jdk 安装路径是/usr/lib/jvm 下面

 

JAVA_HOME指向一个含有java可执行程序的目录(一般是在 bin/java中,此目录为/bin/java的上级目录),用cd 命令进入到 jvm下唯一的一个目录中 java-1.8.0-openjdk-1.8.0.161-0.b14.el7_3.x86_64,发现其下目录为 

/jar/bin/java.jre-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64 这个链接是指向 java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/jre 这个文件夹,所以,可以直接用export命令将 JAVA_HOME 指向

 jre-1.8.0-openjdk-1.8.0.121-0.b14.el7_4.x86_64这个链接.

 

#临时生效

[root@localhost ~]#  export JAVA_HOME=/usr/lib/jvm/<span style="font-family: Arial;">jre-1.8.0-openjdk-1.8.0.121-0.b13.el7_3.x86_64</span> 

#当前用户生效的配置

vim ~/.bashrc
#在文件底部加入下面一句
export  JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64

#如果使所有用户生效的配置

复制代码

vim /etc/profile

 #set java environment  

export JAVA_HOME=/usr/lib/jvm/java

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar

export PATH=$PATH:$JAVA_HOME/bin

复制代码

 

#使得配置生效

. /etc/profile

 

#查看变量

复制代码

[root@localhost ~]#  echo $JAVA_HOME  
/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64

 [root@localhost ~]# echo $CLASSPATH
.:/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/lib/dt.jar:/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64/lib/tools.jar

复制代码

 javac 和java 命令都有输出设置提示就表示安装和环境配置成功了

案例如下:

[root@instanc]# yum -y list java  

Loaded plugins: langpacks, versionlock

Error: No matching Packages to list

[root@instanc]# rpm -qa |grep java

[root@instanc]# rpm -qa |grep jdk

[root@instanc]# rpm -qa |grep gcj

[root@instanc]# yum install java-1.8.0-openjdk* -y

--------中间有安装过程,最后complete

Complete!

[root@instance-ozyu8y37 ~]# java -version

openjdk version "1.8.0_191"

OpenJDK Runtime Environment (build 1.8.0_191-b12)

OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

[root@instance-ozyu8y37 ~]# cd /usr

[root@instance-ozyu8y37 usr]# ls

bin  etc  games  include  lib  lib64  libexec  local  sbin  share  src  tmp

[root@instance-ozyu8y37 usr]# cd lib

[root@instance-ozyu8y37 lib]# cd jvm

[root@instance-ozyu8y37 jvm]# ls

java

java-1.8.0

java-1.8.0-openjdk

java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64

java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64-debug

java-openjdk

jre

jre-1.8.0

jre-1.8.0-openjdk

jre-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64

jre-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64-debug

jre-openjdk

[root@instance-ozyu8y37 jvm]# vim /etc/profile   # 配置java环境变量(所有用户)

# System wide environment and startup programs, for login setup

# Functions and aliases go in /etc/bashrc

 

# It's NOT a good idea to change this file unless you know what you

# are doing. It's much better to create a custom.sh shell script in

# /etc/profile.d/ to make custom changes to your environment, as this

# will prevent the need for merging in future updates.

 

pathmunge () {

    case ":${PATH}:" in

        *:"$1":*)

            ;;

        *)

            if [ "$2" = "after" ] ; then

                PATH=$PATH:$1

            else

                PATH=$1:$PATH

            fi

    esac

}

 

 

if [ -x /usr/bin/id ]; then

    if [ -z "$EUID" ]; then

        # ksh workaround

        EUID=`/usr/bin/id -u`

        UID=`/usr/bin/id -ru`

    fi

    USER="`/usr/bin/id -un`"

    LOGNAME=$USER

    MAIL="/var/spool/mail/$USER"

fi

#set java environment  

 

export JAVA_HOME=/usr/lib/jvm/java

 

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar

 

export PATH=$PATH:$JAVA_HOME/

Logo

更多推荐