安装了新版本的 Java,但仍在使用以前的版本

安装了新版本的 Java,但仍在使用以前的版本

服务器上的 Java 版本是 1.6.0_37,我使用 yum install java-1.7.0-openjdk 安装 Java-1.7,但是使用此命令后,当我使用 java -version 时,Java 版本仍然是 1.6.0。如何将其更改为 1.7?

# yum install java-1.7.0-openjdk


java -version

java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01, mixed mode)

我也按照以下步骤操作,但 Java 版本仍然是 1.6.0_37

Downloading Latest Java Archive

Java latest archive is available on its official site. We recommend to download latest version of Java from Oracle official website.
For 64 Bit:-

# cd /opt/
# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.tar.gz"

For 32 Bit:-

# cd /opt/
# wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-i586.tar.gz"

Note: In any case if above command failed to download and you need to download java through Linux terminal, watch below screen cast ( http://screencast.com/t/wf9bQ0XjDPxT ), You are required a graphical browser.

After completing download, Extract archive using following command. Use archive file as per your system configuration. For this example we are using CentOS 6.6 (64 bit) system.

# tar xzf jdk-7u75-linux-x64.tar.gz

Install Java with Alternatives

After extracting Java archive file, we just need to set up to use newer version of Java using alternatives. Use the following commands to do it.

# cd /opt/jdk1.7.0_75/
# alternatives --install /usr/bin/java java /opt/jdk1.7.0_75/bin/java 2
# alternatives --config java

There are 4 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*  1           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
 + 2           /opt/jdk1.7.0_60/bin/java
   3           /opt/jdk1.7.0_72/bin/java
   4           /opt/jdk1.7.0_75/bin/java

Enter to keep the current selection[+], or type selection number: 4 [Press Enter]

Now you may also required to set up javac and jar commands path using alternatives command.

# alternatives --install /usr/bin/jar jar /opt/jdk1.7.0_75/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.7.0_75/bin/javac 2
# alternatives --set jar /opt/jdk1.7.0_75/bin/jar
# alternatives --set javac /opt/jdk1.7.0_75/bin/javac 

Check Installed Java Version

Use following command to check which version of Java is currently being used by system.

# java -version

java version "1.7.0_75"
Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)

Configuring Environment Variables

Most of Java based application’s uses environment variables to work. Use following commands to set up these variable properly. It’s also good to add following commands to any start-up script like ~/.bashrc or ~/.bash_profile.

    Setup JAVA_HOME Variable

    # export JAVA_HOME=/opt/jdk1.7.0_75

    Setup JRE_HOME Variable

    # export JRE_HOME=/opt/jdk1.7.0_75/jre

    Setup PATH Variable

    # export PATH=$PATH:/opt/jdk1.7.0_75/bin:/opt/jdk1.7.0_75/jre/bin

我也使用了以下但仍然显示以前的版本

root@server [/]# yum search java | grep -i --color JDK
ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk
icedtea-web.x86_64 : Additional Java components for OpenJDK - Java browser
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
ldapjdk.x86_64 : The Mozilla LDAP Java SDK
root@server [/]# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: centos.hyve.com
 * extras: centos.hyve.com
 * updates: centos.hyve.com
Package 1:java-1.7.0-openjdk-1.7.0.75-2.5.4.0.el6_6.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package java-1.7.0-openjdk-devel.x86_64 1:1.7.0.75-2.5.4.0.el6_6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                    Arch     Version                    Repository
                                                                           Size
================================================================================
Installing:
 java-1.7.0-openjdk-devel   x86_64   1:1.7.0.75-2.5.4.0.el6_6   updates   9.4 M

Transaction Summary
================================================================================
Install       1 Package(s)

Total download size: 9.4 M
Installed size: 36 M
Is this ok [y/N]: y
Downloading Packages:
java-1.7.0-openjdk-devel-1.7.0.75-2.5.4.0.el6_6.x86_64.r | 9.4 MB     00:03     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : 1:java-1.7.0-openjdk-devel-1.7.0.75-2.5.4.0.el6_6.x86_64     1/1 
  Verifying  : 1:java-1.7.0-openjdk-devel-1.7.0.75-2.5.4.0.el6_6.x86_64     1/1 

Installed:
  java-1.7.0-openjdk-devel.x86_64 1:1.7.0.75-2.5.4.0.el6_6                      

Complete!
root@server [/]# ls -l /usr/lib/vim
/bin/ls: cannot access /usr/lib/vim: No such file or directory
root@server [/]# ls -l /usr/lib/jvm/
total 12
drwxr-xr-x  3 root root 4096 Feb  6 00:03 ./
drwxr-xr-x 28 root root 4096 Feb  5 22:47 ../
lrwxrwxrwx  1 root root   32 Feb  6 00:03 java-1.7.0 -> /etc/alternatives/java_sdk_1.7.0/
drwxr-xr-x  7 root root 4096 Feb  6 00:03 java-1.7.0-openjdk-1.7.0.75.x86_64/
lrwxrwxrwx  1 root root   34 Feb  6 00:03 java-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.75.x86_64/
lrwxrwxrwx  1 root root   34 Feb  6 00:03 java-openjdk -> /etc/alternatives/java_sdk_openjdk/
lrwxrwxrwx  1 root root   21 Feb  5 23:52 jre -> /etc/alternatives/jre/
lrwxrwxrwx  1 root root   27 Feb  5 22:47 jre-1.7.0 -> /etc/alternatives/jre_1.7.0/
lrwxrwxrwx  1 root root   38 Feb  5 22:47 jre-1.7.0-openjdk.x86_64 -> java-1.7.0-openjdk-1.7.0.75.x86_64/jre/
lrwxrwxrwx  1 root root   29 Feb  5 22:47 jre-openjdk -> /etc/alternatives/jre_openjdk/
root@server [/]# vim .bashrc
root@server [/]# java -version
java version "1.6.0_37"
Java(TM) SE Runtime Environment (build 1.6.0_37-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01, mixed mode)
root@server [/]# cd $HOME
root@server [~]# vim .bashrc

答案1

我没有使用 CentOS,但从输出来看,我认为您需要安装 Java 1.7 JRE(运行时环境)。安装 JRE 后,您可以运行alternatives以将默认 Java 可执行文件从 1.6 更改为 1.7。

相关内容