Mac OSX Java 终端版本不正确 [英] Mac OSX Java Terminal version incorrect

查看:45
本文介绍了Mac OSX Java 终端版本不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我对 Mac 和 OSX 有点陌生,但我选择了一个,以便我可以用一个对我的 Java 程序进行一些故障排除,因为我工作的公司使用 OSX 和 Windows 机器的组合.我遇到的问题是,当我从 Oracle 的网站安装 Java 7 时,它会更新首选项菜单并在双击它们时似乎正确执行 .jar 文件,但终端窗口的版本仍然是 1.6.0_43 并运行由于旧版本,来自终端的相同 .jar 文件会导致运行时错误.

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.

当我导航到/Library/Java/JavaVirtualMachines/时,会看到一个空文件夹.从我在其他文章中看到的内容来看,这就是 Java 1.7.0 的版本文件夹所在的位置.知道发生了什么吗?如何让终端使用正确版本的 Java?

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?

@DWilches 评论他的原始答案:(1)

@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

对不起,新答案的错误,太习惯了这么长时间后阻止编辑原始帖子的网站......

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

推荐答案

JDK

在 Mac OS 上,/usr/bin/java 和朋友是委托给真正 JDK 命令的存根.这些存根尊重您的 JAVA_HOME 环境变量的设置,但要使其工作,您需要安装 JDK(来自 http://www.oracle.com/technetwork/java/javase/downloads/index.html) 而不是 JRE(来自http://java.com).

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).

JDK 安装到 /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk(对于 NN 的任何值),因此将 JAVA_HOME 环境变量设置为 /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk/Contents/Home 使 /usr/bin/java 使用 1.7.您只需将 JAVA_HOME 指向 /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 即可切换回 1.6.您可以使用 /usr/libexec/java_home 工具自动找到正确的值,例如让 /usr/bin/java 使用 Java 7 就可以了

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*'`

并使其使用 Java 6,您可以这样做

and to make it use Java 6 you can do

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

这同样适用于 Java 8(使用 -v '1.8*').这将为相关主要版本选择最新安装的 JDK,您无需记住在安装更新时手动更改 NN.

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.

如果您想从命令行运行 1.7 或 1.8 JRE,可以在 /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin 中找到/java.这是一条固定路径,在任何给定时间您只能安装一个公共"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)

您可以在 .bashrc

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

这篇关于Mac OSX Java 终端版本不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆