mac java version版本不对,Mac OSX Java终端版本不正确

Ok, I'm a bit new to Macs and OSX, but I picked one up so that I can do some troubleshooting on my Java programs with one since the company I work for uses a combination of OSX and Windows machines. The problem I'm running into is, when I install Java 7 from Oracle's website, it updates the preferences menu and appears to execute .jar files correctly when double-clicking them, but the terminal window's version is still 1.6.0_43 and running the same .jar file from the terminal results in runtime errors due to the older version.

When I navigate to /Library/Java/JavaVirtualMachines/ I'm presented with an empty folder. From what I've seen in other articles, this is where the Java 1.7.0's version folder should be. Any idea what's going on? How can I get the terminal to use the correct version of Java?

Edit: @DWilches comment on his original answer:

(1)

total 64

lrwxr-xr-x 1 root wheel 10 Mar 17 21:38 1.4 -> CurrentJDK

lrwxr-xr-x 1 root wheel 10 Mar 17 21:38 1.4.2 -> CurrentJDK

lrwxr-xr-x 1 root wheel 10 Mar 17 21:38 1.5 -> CurrentJDK

lrwxr-xr-x 1 root wheel 10 Mar 17 21:38 1.5.0 -> CurrentJDK

lrwxr-xr-x 1 root wheel 10 Mar 17 21:38 1.6 -> CurrentJDK

lrwxr-xr-x 1 root wheel 10 Mar 17 21:38 1.6.0 -> CurrentJDK

drwxr-xr-x 8 root wheel 272 Mar 17 21:38 A

lrwxr-xr-x 1 root wheel 1 Mar 17 21:38 Current -> A

lrwxr-xr-x 1 root wheel 59 Mar 17 21:38 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

(2)

ls -ld /usr/bin/java

lrwxr-xr-x 1 root wheel 74 Mar 17 21:38 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

Edit: sorry for the mistake with a new answer, gotten too used to sites that block edits of the original post after so long...

解决方案

JDK

On Mac OS, /usr/bin/java and friends are stubs that delegate to the real JDK commands. These stubs respect the setting of your JAVA_HOME environment variable, but for this to work you need to install the JDK (from http://www.oracle.com/technetwork/java/javase/downloads/index.html) as opposed to the JRE (from http://java.com).

The JDK installs into /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk (for whatever value of NN), so set your JAVA_HOME environment variable to /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk/Contents/Home to make /usr/bin/java use 1.7. You can switch back to 1.6 simply by pointing your JAVA_HOME to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home instead. You can use the /usr/libexec/java_home tool to find the right value automatically, for example to make /usr/bin/java use Java 7 you can do

export JAVA_HOME=`/usr/libexec/java_home -v '1.7*'`

and to make it use Java 6 you can do

export JAVA_HOME=`/usr/libexec/java_home -v '1.6*'`

The same applies to Java 8 (using -v '1.8*'). This will pick up the latest installed JDK for the relevant major version, you don't need to remember to change the NN by hand when you install an update.

JRE

If you want to run the 1.7 or 1.8 JRE from the command line, it can be found in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java. This is a fixed path and you can only have one "public" JRE installed at any given time.

$ /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version

java version "1.7.0_13"

Java(TM) SE Runtime Environment (build 1.7.0_13-b20)

Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

You could use a shell alias in your .bashrc

alias java_jre='/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值