简体   繁体   English

Mac OSX Java终端版本不正确

[英]Mac OSX Java Terminal version incorrect

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

When I navigate to /Library/Java/JavaVirtualMachines/ I'm presented with an empty folder. 当我导航到/ Library / Java / JavaVirtualMachines /我会看到一个空文件夹。 From what I've seen in other articles, this is where the Java 1.7.0's version folder should be. 从我在其他文章中看到的,这是Java 1.7.0的版本文件夹所在的位置。 Any idea what's going on? 知道发生了什么事吗? How can I get the terminal to use the correct version of Java? 如何让终端使用正确版本的Java?

Edit: @DWilches comment on his original answer: (1) 编辑:@DWilches评论他的原始答案:(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) (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 JDK

On Mac OS, /usr/bin/java and friends are stubs that delegate to the real JDK commands. 在Mac OS上, /usr/bin/java和朋友是委托给真正的JDK命令的存根。 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 ). 这些存根遵循您的JAVA_HOME环境变量的设置,但为此,您需要安装JDK (来自http://www.oracle.com/technetwork/java/javase/downloads/index.html )而不是JRE (来自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. JDK安装到/Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk (对于NN的任何值),因此将您的JAVA_HOME环境变量设置为/Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk/Contents/Home make /usr/bin/java使用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. 您只需将JAVA_HOME指向/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home即可切换回1.6。 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 您可以使用/usr/libexec/java_home工具自动查找正确的值,例如使/usr/bin/java使用Java 7你可以做

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

and to make it use Java 6 you can do 并且可以使用Java 6

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

The same applies to Java 8 (using -v '1.8*' ). 这同样适用于Java 8(使用-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. 这将为相关主要版本选择最新安装的JDK,您无需记住在安装更新时手动更改NN

JRE 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 . 如果要从命令行运行1.7或1.8 JRE ,可以在/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. 这是一条固定路径,您只能在任何给定时间安装一个“公共”JRE。

$ /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 您可以在.bashrc使用shell别名

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

The issue is that Oracle's JRE installation does not change the /usr/bin/java executable. 问题是Oracle的JRE安装不会更改/ usr / bin / java可执行文件。 If you want to use this Java you have to use the path under /Library (need to find what this is I have JDK install so could be different) 如果你想使用这个Java,你必须使用/ Library下的路径(需要找到我有JDK安装的东西,所以可能会有所不同)

As per Oracle's JRE installation document 根据Oracle的JRE 安装文档

Installing a JRE from Oracle will not update java -version symlinks or add java to your path. 从Oracle安装JRE不会更新java -version符号链接或将java添加到路径中。 To be able to do this, you need to install the JDK. 为此,您需要安装JDK。

The Oracle JDK does change /usr/bin/java to point to the Java 7 executable. Oracle JDK确实将/usr/bin/java更改为指向Java 7可执行文件。 If you are doing development then you should use this as it includes more than the JRE. 如果您正在进行开发,那么您应该使用它,因为它包含的不仅仅是JRE。

The Oracle install FAQ says Oracle 安装常见问题解答

Q: Should I install the JRE or the JDK? 问:我应该安装JRE还是JDK?

A: If you plan to run Java applications, install the Java Runtime Environment (JRE). 答:如果您计划运行Java应用程序,请安装Java Runtime Environment(JRE)。 The JRE is also referred to as Oracle Java. JRE也称为Oracle Java。 Once you have installed the JRE, you can launch Java applets and applications by double-clicking JAR files, JNLP files, and via the browser. 安装JRE后,可以通过双击JAR文件,JNLP文件和浏览器来启动Java applet和应用程序。 Note that 32-bit browsers, such as Firefox in 32-bit mode, and Chrome, are not supported by the JRE. 请注意,JRE不支持32位浏览器,例如32位模式下的Firefox和Chrome。

If you plan to write Java applications, install the Java Development Kit (JDK). 如果您计划编写Java应用程序,请安装Java Development Kit(JDK)。

First, did you Quit the Console and open it again so it has chances of taking the changes in the PATH variable ? 首先,您是否退出控制台并再次打开它,以便它有可能在PATH变量中进行更改?

If you've already done this, then check which the default version of your Java is: 如果您已经这样做了,那么请检查Java的默认版本:

dwilches@ ~$ cd /System/Library/Frameworks/JavaVM.framework/Versions/
dwilches@ Versions$ ls -l
lrwxr-xr-x  1 root  wheel   59 Mar 19 10:07 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.6.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.6 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.5.0 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.5 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.4.2 -> CurrentJDK
lrwxr-xr-x  1 root  wheel   10 Mar 19 10:07 1.4 -> CurrentJDK
lrwxr-xr-x  1 root  wheel    1 Mar 19 10:08 Current -> A
drwxr-xr-x  8 root  wheel  272 Mar 25 10:03 A

Now, you can see that the "Current" version is "A" (that's my Java7). 现在,您可以看到“当前”版本是“A”(这是我的Java7)。 If I would like to change it to Java 1.6 then I could write: 如果我想将其更改为Java 1.6,那么我可以写:

dwilches@ Versions$ sudo unlink Current
dwilches@ Versions$ sudo ln -s 1.6 Current

And then: 然后:

dwilches@ Versions$ java -version
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01-447-11M4203)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01-447, mixed mode)

So you may use this to make "Current" point to your desired Java location. 因此,您可以使用它来使“当前”指向您想要的Java位置。

Just like Ian said, but probably you want java7 to be called from .sh script, so you need a function instead of alias added to .bash_profile: 就像Ian所说,但是你可能希望从.sh脚本调用java7,所以你需要一个函数而不是别名添加到.bash_profile:

java7() {
    /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java "$@"
}
export -f java7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM