Mac OSX Java Terminal version is incorrect

Ok, I'm a little new to Mac and OSX, but I chose one so that I can perform some troubleshooting problems on my Java programs, as the company I work with uses a combination of OSX and Windows machines. The problem I am facing is that when I install Java 7 from the Oracle website, it updates the settings menu and seems to correctly execute .jar files when double-clicking on them, but the version of the terminal window is still equal 1.6.0_43 and runs the same .jar file from the terminal leading to runtime errors due to an older version.

When I go to / Library / Java / JavaVirtualMachines /, I get an empty folder. From what I saw in other articles, this is where the folder for Java version 1.7.0 should be. Any idea what is going on? How can I make the terminal use the correct version of Java?

Edit: @DWilches comment on the 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 error with the new answer, I'm too used to sites that have been blocking the editing of the original message for so long ...

+51
java terminal macos
Mar 25 '13 at 21:00
source share
4 answers

Jdk

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

The JDK is installed in /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk (for any NN value), so set the JAVA_HOME environment variable to /Library/Java/JavaVirtualMachines/jdk1.7.0_NN.jdk/Contents/Home to do /usr/bin/java use 1.7. You can go back to 1.6 simply by pointing JAVA_HOME instead to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home . You can use the /usr/libexec/java_home to automatically find the correct value, for example to make /usr/bin/java use Java 7, which you can do

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

and so that it uses Java 6 you can do

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

The same goes for Java 8 (using -v '1.8*' ). This will select the latest installed JDK for the corresponding major version, you should not forget to change NN manually when installing the update.

Jre

If you want to run 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 install only one โ€œpublicโ€ JRE 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 can use a shell alias in your .bashrc

 alias java_jre='/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java' 
+99
Mar 25 '13 at 21:57
source share

The problem is that installing the Oracle JRE does not modify the / usr / bin / java executable. If you want to use this Java, you need to use the path in / Library (you need to find what it is, I have JDK installed, so it may be different)

According to Oracle JRE installation document

Installing a JRE from Oracle will not update java -version symbolic links or add java to your path. To do this, you need to install the JDK.

The Oracle JDK changes /usr/bin/java to point to the Java 7 executable. If you are developing, you should use this because it includes more than the JRE.

Oracle install frequently asked questions says

Q: Should I install JRE or JDK?

A: If you plan to run Java applications, install the Java Runtime Environment (JRE). JRE is also referred to as Oracle Java. Once you installed the JRE, you can run Java applets and applications by double-clicking the JAR files, JNLP files, and through the browser. Please note that 32-bit browsers such as Firefox in 32-bit mode and Chrome are not supported by the JRE.

If you plan to write Java applications, install the Java Development Kit (JDK).

+9
Mar 25 '13 at 21:47
source share

First you left the console and reopened it to be able to make changes to the PATH variable?

If you have already done this, check which version of Java is by default:

 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โ€ (this is my Java7). If I wanted to change it to Java 1.6, I could write:

 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 can use this to make the โ€œCurrentโ€ point in your desired Java location.

+5
Mar 25 '13 at 21:13
source share

Like Ian, but you probably want java7 to be called from a .sh script, so you need a function instead of an alias added to .bash_profile:

 java7() { /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java "$@" } export -f java7 
-one
Nov 09 '13 at 22:56
source share



All Articles