pages tagged mac_os_x http://meng6net.localhost/tag/mac_os_x/ <p><small>Copyright © 2005-2020 by <code>Meng Lu &lt;lumeng3@gmail.com&gt;</code></small></p> Meng Lu's home page ikiwiki Tue, 16 May 2017 23:59:39 +0000 Installing and configuring Java on macOS http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Java_on_macOS/ http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_Java_on_macOS/ Java computing configuration documentation installation java macOS mac os x note software tutorial Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <p>This is a note on my experience of installing Java on macOS 10.9.2.</p> <p>macOS has its own system Java which as of macOS 10.9.2 is at version 1.6.0_65. It's more or less consistent with Java 6 (<a href="http://docs.oracle.com/javase/specs/">Java Language and Virtual Machine Specifications</a> version <a href= "http://docs.oracle.com/javase/specs/">SE 6</a>). I'd like to keep macOS's system Java for various tasks and applications such as Mathematica which requires on Java 6. It's said that Apple no longer supports Java as a system component, so it's possile there will not be future major version update to Java 7 and higher.</p> <p>The new "Java" I'd like to install is Oracle's Java. Oracle calls it "Java Platform (JDK) Platform". JDK is Java Development Kit which is the software package one needs to do Java software development. It's currently at version "Java SE 7" and is updated to "JDK SE 8" in March 2014. I'd like to install it for learning new Java features and used it in some projects.</p> <h2>macOS's system Java</h2> <h3>The 'java' executable and Java Virtual Machines</h3> <p>The default command-line <code>java</code> on macOS 10.9.2 is version <code>1.6.0_65</code>:</p> <pre><code>$ which java /usr/bin/java $ java -version java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode) </code></pre> <p>This <code>java</code> along with other Java related binaries in the same path <code>/usr/bin/</code> are actually symbolic links pointing to OS X's default installation directory for Java which in turn is part of system libraries <code>/System/Library/</code>:</p> <pre><code>$ ls -l /usr/bin/*java* lrwxr-xr-x 1 root wheel 74 Nov 3 10:29 /usr/bin/java -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java lrwxr-xr-x 1 root wheel 75 Nov 3 10:29 /usr/bin/javac -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac lrwxr-xr-x 1 root wheel 77 Nov 3 10:29 /usr/bin/javadoc -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javadoc lrwxr-xr-x 1 root wheel 75 Nov 3 10:29 /usr/bin/javah -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javah lrwxr-xr-x 1 root wheel 75 Nov 3 10:29 /usr/bin/javap -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javap lrwxr-xr-x 1 root wheel 76 Nov 3 10:29 /usr/bin/javaws -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javaws </code></pre> <p>So the <code>java</code> at command line is actually using <code>/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java</code>. The <code>Current</code> directory is actually also a symbolic link:</p> <pre> <code>$ ls -la /System/Library/Frameworks/JavaVM.framework/Versions/ total 64 drwxr-xr-x 11 root wheel 374 Nov 3 10:30 . drwxr-xr-x 12 root wheel 408 Nov 3 10:30 .. lrwxr-xr-x 1 root wheel 10 Nov 3 10:29 1.4 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Nov 3 10:29 1.4.2 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Nov 3 10:29 1.5 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Nov 3 10:29 1.5.0 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Nov 3 10:29 1.6 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Nov 3 10:29 1.6.0 -&gt; CurrentJDK drwxr-xr-x 8 root wheel 272 Nov 3 10:30 A lrwxr-xr-x 1 root wheel 1 Nov 3 10:29 Current -&gt; A lrwxr-xr-x 1 root wheel 59 Nov 3 10:29 CurrentJDK -&gt; /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents </code></pre> <p>So this further mean the <code>java</code> at command line is actually using <code>/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java</code>:</p> <pre> <code>$ ls -la /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java -rwxr-xr-x 1 root wheel 54624 Nov 3 10:29 /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java $ /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java -version java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode) </code></pre> <p>Difference between <code>/System/Library/Frameworks/</code> and <code>/Library/</code>:</p> <p>The <code>/System/Library/Frameworks/</code> path is where macOS installs its essential framework packages such as Java. The framework packages are shared by multiple applications. Different applications may need different versions of the same package so in general there can be multiple versions of the same package. Third-party libraries is installed in <code>/Library/</code>.</p> <p><code>/System/Library/Java/</code>:</p> <p>All the previous versions of Java {<code>1.4</code>, <code>1.5</code>, ..., <code>1.6.0</code>} is redirected to <code>CurrentJDK</code> and then to <code>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents</code>, perhaps for backwards compatibility. And the "current" Java is pointed to <code>Current/</code> and then to <code>A/</code>. I guess 'A' stands for "Automatic". Also, I noted that there is one Apple's Java VM:</p> <pre><code>$ ls -la /System/Library/Java/JavaVirtualMachines/ total 0 drwxr-xr-x 3 root wheel 102 Oct 10 11:13 . drwxr-xr-x 5 root wheel 170 Nov 3 10:30 .. drwxr-xr-x 3 root wheel 102 Oct 10 11:13 1.6.0.jdk </code></pre> <p>I'm not sure what's the relation between <code>/System/Library/Frameworks/JavaVM.framework/</code> and <code>/System/Library/Java</code>. My guess is that <code>JavaVM.framework</code> is newly introduced and used 1) as an "interface" which maintain various links to <code>/System/Library/Java/</code> for previous versions; 2) to store the "current" version of system "Java" in <code>/System/Library/Frameworks/JavaVM.framework/Versions/A/</code>.</p> <p>Apple has a web site that provides <a href= "http://support.apple.com/downloads/#java">downloading of macOS system Java</a>.</p> <h3><code>JAVA_HOME</code> environment variable</h3> <pre><code>$ echo $JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home $ ls -la /System/Library/Frameworks/JavaVM.framework/Home lrwxr-xr-x 1 root wheel 24 Apr 1 19:36 /System/Library/Frameworks/JavaVM.framework/Home -&gt; Versions/CurrentJDK/Home $ ls -la /System/Library/Frameworks/JavaVM.framework/Versions/ total 64 drwxr-xr-x 11 root wheel 374 Apr 1 19:37 . drwxr-xr-x 12 root wheel 408 Apr 1 19:37 .. lrwxr-xr-x 1 root wheel 10 Apr 1 19:36 1.4 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Apr 1 19:36 1.4.2 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Apr 1 19:36 1.5 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Apr 1 19:36 1.5.0 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Apr 1 19:36 1.6 -&gt; CurrentJDK lrwxr-xr-x 1 root wheel 10 Apr 1 19:36 1.6.0 -&gt; CurrentJDK drwxr-xr-x 8 root wheel 272 Apr 1 19:37 A lrwxr-xr-x 1 root wheel 1 Apr 1 19:36 Current -&gt; A lrwxr-xr-x 1 root wheel 59 Apr 1 19:36 CurrentJDK -&gt; /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents </code></pre> <p>Notice that it does not point to a <code>Home</code> inside <code>/System/Library/Frameworks/JavaVM.framework/Versions/A/</code>. Actually <code>A/</code> does not contain a <code>Home/</code>. I'm not sure why there are both <code>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Commands</code> (which points to <code>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/</code>) and <code>/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands</code>.</p> <h3>(Re-)install macOS's system Java</h3> <p>If for some reason you need to (re-)install Apple's system Java, you can download it from Apple's support site <a href= "http://support.apple.com/downloads/#java">http://support.apple.com/downloads/#java</a>. For example, as of April 2014, I will choose "Java for OS X 2013-005" for macOS 10.9.2.</p> <p>There is also packages called <a href= "https://developer.apple.com/downloads/index.action?name=Java%20Developer"> "Java for macOS 10.6 Update 17 Developer Package" or "Java for OS X 2013-005 Developer Package"</a> that "contains JavaDoc, tools documentation, and native framework headers". I've never tried these.</p> <h3>Preventing future macOS update to Java</h3> <p>If you decide to use Oracle JDK exclusively in future, it maybe desirable to more "thoroughly" delete Apple's Java. This is what I've not attempted to do.</p> <p>The possible removals you'd need are</p> <pre><code>$ rm -rf /System/Library/Java/JavaVirtualMachines/ $ rm /private/var/db/receipts/com.apple.pkg.JavaForMacOSX* </code></pre> <p>and edit the installation receipts <code>/Library/Receipts/InstallHistory.plist</code> and remove the <code>&lt;dict&gt;</code> XML elements that has <code>com.apple.Java*</code> in it:</p> <pre> <code>$ ack "com\.apple.*ava.*" /Library/Receipts/InstallHistory.plist &lt;string&gt;com.apple.pkg.JavaSDKLeo&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6Update3&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6Update4&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6Update5&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX10.6&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaTools&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaEssentials&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaEssentials&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX107&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaMDNS&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaEssentials&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX107&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaMDNS&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaTools&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaEssentials&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaEssentials&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaForMacOSX107&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaSecurity&lt;/string&gt; &lt;string&gt;com.apple.pkg.JavaMDNS&lt;/string&gt; </code></pre> <h2>Installing Oracle's Java</h2> <h3>The path <code>/Library/Java/</code></h3> <p>Third-party Java will be installed to <code>/Library/Java</code>. Note there is not a <code>/System/</code> in front of it.</p> <pre><code>$ ls -la /Library/Java total 8 drwxrwxr-x 5 root admin 170 Nov 3 10:30 . drwxr-xr-x+ 72 root wheel 2448 Mar 8 08:08 .. drwxrwxr-x 2 root admin 68 Nov 3 10:30 Extensions lrwxr-xr-x 1 root wheel 48 Nov 3 10:29 Home -&gt; /System/Library/Frameworks/JavaVM.framework/Home drwxr-xr-x 2 root wheel 68 Nov 3 10:30 JavaVirtualMachines $ ls -la /Library/Java/JavaVirtualMachines/ total 0 drwxr-xr-x 2 root wheel 68 Nov 3 10:30 . drwxrwxr-x 5 root admin 170 Nov 3 10:30 .. </code></pre> <p>The symbolic link <code>Home -&gt; /System/Library/Frameworks/JavaVM.framework/Home</code> is how macOS by default manages the <code>$JAVA_HOME</code> environment variable for third-party Java. <code>$JAVA_HOME</code> by default is set to <code>/Library/Java/Home</code> which is updated to point to the current version of Java <code>/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/</code>.</p> <p>Currently I have no third-party Java and hence an empty <code>/Library/Java/JavaVirtualMachines/</code>.</p> <p>To install a new non-system non-Apple Java on Mac, one can install <a href= "http://www.oracle.com/technetwork/java/javase/downloads/index.html"> Oracle's JDK</a>, and possibly <a href= "http://openjdk.java.net/projects/macosx-port/">OpenJDK</a>). These have versions newer than Apple's Java.</p> <p>My goal at the time of writing this note is to install the newer Java (Java Language and Virtual Specification, version SE 7 or even version "Java SE 8") while keep macOS's own system Java (version "Java SE 6"), since I need both for different work.</p> <h3>Installing Oracle's JDK</h3> <p>To install "Java 7", download "Java SE Development Kit 7" for "macOS x64" from <a href= "http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html"> Oracle</a>. Install that. After that, you should find <code>/Library/Java/JavaVirtualMachines/</code> has the newly installed Oracle JDK:</p> <pre><code>$ ls /Library/Java/JavaVirtualMachines jdk1.7.0_51.jdk $ /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java -version java version "1.7.0_51" Java(TM) SE Runtime Environment (build 1.7.0_51-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode) </code></pre> <p>But this does not affect the default command-line <code>java</code>:</p> <pre><code>$ java -version java 1.6.0_65 </code></pre> <p>Now you have both Java 6 and 7 on the computer and can use them for different purposes.</p> <h3>Making Oracle Java binaries available at command line</h3> <p>Depending on if it's desirable to you, you may want to link Java related binaries in <code>/usr/bin</code></p> <pre><code>$ ls -la /usr/bin/*java* lrwxr-xr-x 1 root wheel 74 Nov 3 10:29 /usr/bin/java -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java lrwxr-xr-x 1 root wheel 75 Nov 3 10:29 /usr/bin/javac -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac lrwxr-xr-x 1 root wheel 77 Nov 3 10:29 /usr/bin/javadoc -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javadoc lrwxr-xr-x 1 root wheel 75 Nov 3 10:29 /usr/bin/javah -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javah lrwxr-xr-x 1 root wheel 75 Nov 3 10:29 /usr/bin/javap -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javap lrwxr-xr-x 1 root wheel 76 Nov 3 10:29 /usr/bin/javaws -&gt; /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javaws </code></pre> <p>to Oracle's Java, either under a different name, say <code>java7</code>, or the default name <code>java</code>, and optionally rename the existing macOS's <code>java</code> to <code>java6</code> first. I created <code>java7</code>:</p> <pre> <code>$ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/java /usr/bin/java7 $ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/javac /usr/bin/javac7 ... </code></pre> <p>And you probably want to keep <code>$JAVA_HOME</code> consistent with <code>java</code>. If <code>java</code> is pointing to Oracle Java, you'd want</p> <pre> <code>$ ln -s /Library/Java/Home /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home </code></pre> <h2>Managing <code>JAVA_HOME</code> for different environments</h2> <p><code>JAVA_HOME</code> is a UNIX envrionment variable that are sometimes used to determine which and how Java Virtual Machine is run. It's a somewhat complex issue to set them appropriately for different purposes.</p> <p>Here is what I've done:</p> <h3>Setting <code>JAVA_HOME</code> for Bash</h3> <p>Add the following code to shell configuration file. There are <a href= "http://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x"> multiple configuration files for Bash shell including <code>.profile</code>, <code>.bashrc</code>, <code>/etc/profile</code></a>. For me, I used <code>$HOME/.bashrc</code>:</p> <pre> <code>## Method 1: Set JAVA_HOME to macOS's system Java Home/ which is a symlink pointing to ## /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home as of macOS 10.9.2 ## When there are multiple versions of Java installed, using the /usr/libexec/java_home method below is better. # export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Home" ## Method 2: Alternatively, set the path using the /usr/libexec/java_home command with a parameter -v to choose specific version of Java ## Use the following to see verbose info about available versions of Java when unsure. ## /usr/libexec/java_home -V ## /usr/libexec/java_home -Vv 1.6 ## Use Java 6 export JAVA_HOME=`/usr/libexec/java_home -v 1.6` ## Use Java 7 # export JAVA_HOME=`/usr/libexec/java_home -v 1.7` ## Use Java 8 # export JAVA_HOME=`/usr/libexec/java_home -v 1.8` </code></pre> <p>So, potentially, one can tweak the lines in method 2 and choose different version to use. Notice that the binary <code>/usr/libexec/java_home</code> is used to determine the Java home directory:</p> <pre><code>$ /usr/libexec/java_home -V Matching Java Virtual Machines (4): 1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home 1.7.0_51, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_51/Contents/Home 1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home </code></pre> <p>It seems it recognizes all available JVMs and picks the home directory from the latest version.</p> <h3>Setting <code>JAVA_HOME</code> for Mathematica</h3> <p>I'm a heavy user of <a href= "http://www.wolfram.com/mathematica/">Mathematica</a>. As of Mathematica 9, it is developed based on Java 6 and its Java related functionalities such as J/Link package may not work properly if a different version of Java is used.</p> <p>There are two configurations to set and/or check.</p> <h4><code>JAVA_HOME</code></h4> <p>As of Mathematica 9, verify that Mathematica's <code>$JAVA_HOME</code> is pointing to macOS's system Java's JVM 1.6.*:</p> <pre><code>In[1]:= Import["!echo $JAVA_HOME", "Text"] Out[1]= "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" In[2]:= Environment["JAVA_HOME"] Out[2]= "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" </code></pre> <p>I'm not sure why the following does not work:</p> <pre><code>In[3]:= Import["!java -version", "Text"] Out[3]= "" </code></pre> <h4><code>CommandLine -&gt; "java"</code></h4> <p>One can <a href= "https://reference.wolfram.com/mathematica/JLink/tutorial/CallingJavaFromMathematica.html"> "install" Java inside Mathematica runtime by the function <code>InstallJava</code> and then use Java classes inside Mathematica programs.</a></p> <p>The <code>InstallJava</code> function or its close relative <code>ReinstallJava</code> has an option called <a href= "https://reference.wolfram.com/mathematica/JLink/tutorial/CallingJavaFromMathematica.html"> <code>CommandLine</code></a>:</p> <pre> <code>CommandLine -&gt; "cmd" use the specified command line to launch the Java runtime, instead of "java" </code></pre> <p>Some examples of that is</p> <pre><code>InstallJava[] InstallJava[CommandLine -&gt; "java"] InstallJava[CommandLine -&gt; "java --Xmx1024m"] </code></pre> <p>The source code in <code>$InstallationDirectory/SystemFiles/Links/JLink/Kernel/InstallJava.m</code> indicates that if <code>CommandLine</code> is not used, it will uses Mathematica's own Java <code>ToFileName[{getJLinkAppDir[], "JLink.app", "Contents", "MacOS"}, "JavaApplicationStub"]</code>. If <code>CommandLine -&gt; "java"</code> is used, make sure it's the right version:</p> <pre><code>In[8]:= Needs["JLink`"] ReinstallJava[CommandLine-&gt;"java"] LoadJavaClass["java.lang.System"] java`lang`System`getProperty["java.version"] Out[9]= LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000347291",166,4] Out[10]= JavaClass[java.lang.System,&lt;&gt;] Out[11]= 1.6.0_65 </code></pre> <p>If the output of <code>getProperty["java.version"]</code> is not consistent with <code>JAVA_HOME</code>, or if <code>LoadJavaClass["java.lang.System"]</code> issues errors, there must be some problem.</p> <p>See <a href="http://meng6net.localhost/tag/mac_os_x/installing_and_configuring_java_on_mac.nb">the attached notebook containing these testing code</a>.</p> <h4>Methods for starting Mathematica</h4> <h5><code>open -a Mathematica</code></h5> <p>If I start Mathematica with <code>open -a Mathematica</code> from command line, which would lead Mathematica to inherit <code>JAVA_HOME</code> set in <code>.bashrc</code>, I get the correct <code>JAVA_HOME</code> value</p> <pre><code>In[1]:= Import["!echo $JAVA_HOME", "Text"] Out[1]= "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/\ Home" In[2]:= Environment["JAVA_HOME"] Out[2]= "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/\ Home" </code></pre> <p>as well as correct version of Java:</p> <pre><code>In[16]:= Needs["JLink`"] ReinstallJava[CommandLine -&gt; "java"] LoadJavaClass["java.lang.System"] java`lang`System`getProperty["java.version"] Out[17]= LinkObject["'/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.app/Contents/MacOS/JavaApplicationStub' -init \"/tmp/m00000847291\"", 219, 4] Out[18]= JLink`JavaClass["java.lang.System", 0, {JLink`JVM["vm1"],JLink`JVM["vm2"]}, 1, "java`lang`System`", False, True] Out[19]= "1.6.0_65" </code></pre> <p>At the same time Oracle JDK 1.7 and 1.8 are installed on the system.</p> <h5>Double clicking "/Applications/Mathematica.app"</h5> <p>If I start Mathematica by double clicking "/Applications/Mathematica.app" in Finder or a link to it in Dock,</p> <pre><code>ReinstallJava[CommandLine -&gt; "java"] LoadJavaClass["java.lang.System"] </code></pre> <p>issues a <code>LinkObject::linkn</code> error, and <code>Environment["JAVA_HOME"]</code> returns <code>$Failed</code>:</p> <pre><code>In[1]:= Needs["JLink`"] ReinstallJava[CommandLine-&gt;"java"] LoadJavaClass["java.lang.System"] java`lang`System`getProperty["java.version"] Out[2]= LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000155721",125,4] During evaluation of In[1]:= LinkObject::linkn: Argument LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000155721",125,4] in LinkReadyQ[LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000155721",125,4]] has an invalid LinkObject number; the link may be closed. &gt;&gt; During evaluation of In[1]:= LinkObject::linkn: Argument LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000155721",125,4] in LinkConnectedQ[LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000155721",125,4]] has an invalid LinkObject number; the link may be closed. &gt;&gt; During evaluation of In[1]:= LinkObject::linkn: Argument LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000155721",125,4] in LinkReadyQ[LinkObject[java -classpath "/Applications/Mathematica.app/SystemFiles/Links/JLink/JLink.jar" -Xdock:name=J/Link -Xmx256m -Djava.system.class.loader=com.wolfram.jlink.JLinkSystemClassLoader com.wolfram.jlink.Install -init "/tmp/m00000155721",125,4]] has an invalid LinkObject number; the link may be closed. &gt;&gt; During evaluation of In[1]:= General::stop: Further output of LinkObject::linkn will be suppressed during this calculation. &gt;&gt; During evaluation of In[1]:= InstallJava::fail: A link to the Java runtime could not be established. &gt;&gt; During evaluation of In[1]:= LoadJavaClass::fail: Java failed to load class java.lang.System. &gt;&gt; Out[3]= $Failed Out[4]= java`lang`System`getProperty[java.version] </code></pre> <p>I don't fully understand what causes the <code>LinkObject::linkn</code> error and its possible relation to <code>JAVA_HOME</code>. My guess is this is due to <code>JAVA_HOME</code> not being correctly set for Mathematica and and somewhere in the processing an implied and incorrect Java home is used, possibly computed by</p> <pre><code>$ /usr/libexec/java_home /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home </code></pre> <p>For both ways of starting Mathematica,</p> <pre> <code>ReinstallJava[] (* notice there is NO CommandLine -&gt; "java" used *) LoadJavaClass["java.lang.System"] </code></pre> <p>works. This is probably expected as <code>CommandLine -&gt; Automatic</code> seems to lead Mathematica to use its own Java <code>$InstallationDirectory/SystemFiles/Links/JLink/JLink.app/Contents/MacOS/JavaApplicationStub</code>. The logic controlling this can be found in source file <code>$InstallationDirectory/SystemFiles/Links/JLink/Kernel/InstallJava.m</code>.</p> <p>So if you have multiple Java installed and if <code>/usr/libexec/java_home</code> finds the wrong version of Java home directory for Mathematica (i.e. not 1.6.*)</p> <pre><code>$ /usr/libexec/java_home /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home </code></pre> <p>you'll need to start Mathematica with <code>open -a Mathematica</code>.</p> <h5>Wrapper application OpenMathematicaAppFromCLIWithJava1.6.app made with Automator</h5> <p>If you prefer double clicking an icon on Dock than opening terminal and typing <code>open -a Mathematica</code>, you can create an application equivalent to setting <code>JAVA_HOME</code> and <code>open -a Mathematica</code> and put a link to it in Dock.</p> <p>1) Start /Applications/Automator.app by double clicking it;</p> <p>2) Choose "Application" and press button "Choose";</p> <p>3) In "Variables" input field, type "Run Shell Script", and double click it;</p> <p>4) In the RHS window, replace the default content 'cat' with</p> <pre><code>export JAVA_HOME=$(/usr/libexec/java_home -v 1.6*) open -a "/Applications/Mathematica.app" </code></pre> <p>5) Save it somewhere as a standalone application. I chose name <code>OpenMathematicaAppFromCLIWithJava1.6.app</code>;</p> <p>6) Optionally, you can make the icon of <code>OpenMathematicaAppFromCLIWithJava1.6.app</code> same as <code>Mathematica.app</code>'s by clicking "Get Info" in the context menu of the two applictions, and copy the icon in the top left part of the pop-up window by selecting the icon by clicking on it and <kbd>Cmd</kbd>-<kbd>C</kbd> and <kbd>Cmd</kbd>-<kbd>V</kbd>;</p> <p>7) Drag <code>OpenMathematicaAppFromCLIWithJava1.6.app</code> onto Dock to create a link to it;</p> <p>8) Start Mathematica by clicking the icon on Dock, and verify that <code>Environment["JAVA_HOME"]</code> and <code>getProperty[java.version]</code> behave correctly.</p> <h2>Configuring IDEs (virtually Eclipse and IntelliJ IDEA)</h2> <p>In IDEs such as Eclipse and IntelliJ IDEA, it's actually easier to use multiple versions of JDK, and different versions for different projects/modules. It's fairly easy to find the related documentation. My understanding is IDEs' JDK version does not have to be same as command-line <code>java</code>. They are basically different and unrelated Java development environments.</p> <p>A relatively more complex issue is, as Eclipse and IntelliJ IDEA themselves are Java applications, what JVM are they running off of? And do you possibly want to start them using Oracle Java?</p> <p>Here are some information:</p> <h3>IntelliJ IDEA</h3> <p><a href= "https://intellij-support.jetbrains.com/entries/23455956-Selecting-the-JDK-version-the-IDE-will-run-under"> Selecting the JDK version the IDE will run under</a>.</p> <h3>Eclipse</h3> <p>(This part about Eclipse is a drat and needs verification.)</p> <h4>Method 1: <code>-vm &lt;Oracle JDK 1.7 Home/ path&gt;</code> in Eclipse.ini</h4> <p>Add something like</p> <pre> <code>-vm /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/ </code></pre> <p>at the end of eclipse.ini.</p> <h4>Method 2: shell script + Automator application</h4> <p>Make a shell script that takes care of <code>JAVA_HOME</code> and JVM:</p> <pre><code>#!/bin/sh export JAVA_HOME=$(/usr/libexec/java_home -v 1.7) LAUNCHER_JAR=/Applications/eclipse/plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar java \ -showversion \ -XX:MaxPermSize=256m \ -Xms1024m \ -Xmx1024m \ -Xdock:icon=/Applications/eclipse/Eclipse.app/Contents/Resources/Eclipse.icns \ -XstartOnFirstThread \ -Dorg.eclipse.swt.internal.carbon.smallFonts \ -Dosgi.requiredJavaVersion=1.5 \ -jar $LAUNCHER_JAR </code></pre> <p>and then make a wrapper application using Automator.</p> <h2>Installing Oracle JDK from command-line</h2> <ul> <li> <p>Download DMG</p> <p>$ wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-macosx-x64.dmg"</p> </li> <li> <p>Mount DMG:</p> <p>$ hdiutil mount jdk-8u60-macosx-x64.dmg</p> </li> <li> <p>Install</p> <p>$ sudo installer -pkg /Volumes/JDK\ 8\ Update\ 60/JDK\ 8\ Update\ 60.pkg -dominfo LocalSystem $ sudo installer -pkg /Volumes/JDK\ 8\ Update\ 60/JDK\ 8\ Update\ 60.pkg -target LocalSystem</p> </li> </ul> <h2>References</h2> <ul> <li> <p><a href= "http://www.cc.gatech.edu/~simpkins/teaching/gatech/cs2340/guides/java7-macosx.html"> Christopher L. Simpkins,〈Java 7 on macOS〉</a></p> </li> <li> <p><a href= "http://www.java.com/en/download/faq/java_mac.xml">〈java.com documentation: Information and system requirements for installing and using Mac Java 7〉</a></p> </li> <li> <p><a href= "http://mathiasbynens.be/notes/shell-script-mac-apps">Mathias Bynens, 〈How to create simple Mac apps from shell scripts〉</a></p> </li> <li> <p><a href= "http://stackoverflow.com/questions/10352715/how-do-i-run-eclipse-using-oracles-new-1-7-jdk-for-the-mac"> Stackoverflow.com thread 〈How do I run Eclipse using Oracle's new 1.7 JDK for the Mac?〉</a></p> </li> <li> <p><a href= "http://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x"> Stackoverflow.com thread 〈Environment variables in macOS〉</a></p> </li> <li> <p>docs.oracle.com, <a href= "https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html"> 〈JDK 8 Installation for OS X〉</a></p> </li> </ul>