服务器上的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
.bashrc的内容
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export JAVA_HOME="/usr/lib/jvm/jre-1.7.0-openjdk.x86_64"
目前.bashrc的outpur如下,但仍在使用相同版本的Java
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
export JAVA_HOME="/usr/lib/jvm/jre-1.7.0-openjdk.x86_64"
export PATH=$PATH:$JAVA_HOME\bin
答案 0 :(得分:1)
我通常会将jdk安装到Linux上并得到像你这样的问题。
我通过编辑~/.bash_profile
文件来解决:
JAVA_HOME=/opt/jdk1.7.0_75
#this should be point to your jdk directory
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
注意:请注意,在重新定义$JAVA_HOME/bin
时,您需要在$PATH
之前添加PATH
然后,运行以下命令重新加载.bash_profile
文件
. ~/.bash_profile
然后您可以重新打开终端并检查Java版本。
答案 1 :(得分:0)
我通过将以下内容添加到CentOS 7中的/etc/profile
来实现了同样的目的。另外,我用Oracle的官方JDK-8替换了我的openjdk,但是,这在实际意义上并不重要。
我建议您按照我在 /etc/profile
文件中所做的更改进行更改。根据系统安装java的位置替换实际路径/位置: -
export JAVA_HOME=/usr/java/default
# point location before Java's bin folder in your system
export JRE_HOME=/usr/java/default/jre
# point location before JRE's bin folder in your system
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
# append those 2 locations to the PATH environment variable
JAVA_HOME 是安装Java /bin
目录的路径,正如您从上面的第一个导出命令中注意到的那样, JRE_HOME 是JRE {{从上面的第二个导出命令可以看到1}}目录。