我已经安装了从oracle网站下载的JDK 7u7。但是安装完成后,终端仍然显示java版本6
$java -version
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)
你知道为什么Java 7没有出现吗?
答:
好的,问题已经解决了。以下是答案:
我发现我的终端有一个.bash_profile, java主变量被设置为1.6
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home"
所以这就是每次我打开新终端窗口时导致问题的行。
只要去掉这条线就能解决问题。您仍然需要遵循@aleroot所说的,但如果这对您不起作用,请检查.bash_profile(或.bashrc)设置文件,以查看您之前是否导出过任何java版本。
在我的案例中,问题是Oracle将它安装到与我习惯的位置不同的位置。
从Oracle下载:http://java.com/en/download/mac_download.jsp?locale=en
Verify that it's installed properly by looking in System Prefs:
Command-Space to open Spotlight, type 'System Preferences', hit enter.
Click Java icon in bottom row. After the Java Control Panel opens, click 'Java' tab, 'View...', and verify that your install worked. You can see a 'Path' there also, which you can sub into the commands below in case they are different than mine.
Verify that the version is as you expect (sub in your path as needed):
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version
Create link from /usr/bin/java to your new install
sudo ln -fs /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java /usr/bin/java
Sanity check your version:
java -version
基本问题:/usr/bin/java最初指向OSX自己提供的(/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java)
我们需要将它指向JDK安装程序下载的那个。以下步骤适用于OSX 10.10.4 Yosemite。
Open System Preferences -> select Java. The Java window opens.
Click on Java tab at the top. Click on 'View' button.
The Java Runtime Environment Settings tab opens as below:
Double click on the Path item and copy the path (cmd+c). This is the latest one installed by the JDK installer/updater. In my case, the path was /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
Open terminal. In this step, we are going to point (symbolic link, ln -s command) the system java binary to the latest one, which we discovered in the previous step. Run the below command:
sudo ln -s /Library/Internet\ Plug-Ins/JavaAppletPlugin。插件/内容/ Home / bin / java /usr/bin/java
这是它。要验证,只需运行
java - version
在终端上。它应该输出您安装/更新到的最新版本。
如果你的机器上有几个Java版本,你想在运行时动态地选择它,也就是说,在我的情况下,我有两个版本:
ls -la /Library/Java/JavaVirtualMachines
drwxr-xr-x 3 root wheel 96B Nov 16 2014 jdk1.7.0_71.jdk/
drwxr-xr-x 3 root wheel 96B Mar 1 2015 jdk1.8.0_31.jdk/
您可以通过修改/etc/profile内容来更改它们。只需在文件末尾添加(或修改)以下两行:
export JAVA_HOME=YOUR_JAVA_PATH/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
在我的情况下,如果我想使用,它应该如下所示:
Java 7:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
Java 8:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
保存文件后,请运行source /etc/profile,它应该可以工作。以下是我使用第一种和第二种选择时的结果:
Java 7:
java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java 8:
java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
如果您的java文件夹位于不同的位置,这个过程是类似的。