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

查看:387
本文介绍了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 / code>而不是。您可以使用 / 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中使用shell别名

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

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

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