Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...