pages tagged tutorial http://meng6net.localhost/tag/tutorial/ <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/tutorial/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> Installing and configuring Gradle http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/ http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/ computing configuration documentation gradle installation java note software tool tutorial Tue, 16 May 2017 23:59:39 +0000 2017-05-16T23:59:39Z <p><a href="http://spring.io/guides/gs/gradle/">Spring Framework guides ''Building Java Projects with Gradle'': http://spring.io/guides/gs/gradle/</a> is a good reference for this topic.</p> <h2>Install Gradle</h2> <p>On macOS, install with Homebrew</p> <pre><code>$ brew install gradle ==&gt; Downloading https://services.gradle.org/distributions/gradle-1.12-bin.zip ######################################################################## 100.0% /usr/local/Cellar/gradle/1.12: 155 files, 44M, built in 106 seconds$ brew install gradle ==&gt; Downloading https://services.gradle.org/distributions/gradle-1.12-bin.zip ######################################################################## 100.0% /usr/local/Cellar/gradle/1.12: 155 files, 44M, built in 106 seconds </code></pre> <p>Verify it's installed successfully:</p> <pre><code>$ which gradle /usr/local/bin/gradle $ gradle -version ------------------------------------------------------------ Gradle 2.0 ------------------------------------------------------------ Build time: 2014-07-01 07:45:34 UTC Build number: none Revision: b6ead6fa452dfdadec484059191eb641d817226c Groovy: 2.3.3 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.6.0_65 (Apple Inc. 20.65-b04-462) OS: macOS 10.9.4 x86_64 </code></pre> <h2>Create Gradle-managed Java projects in IntelliJ IDEA</h2> <p>Really, there are just two easy steps:</p> <p>Create New Project, and then</p> <p><a href= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_1.png"> <img src= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_1.png" width="900" height="392.037470725995" class="img" /></a> ☝ step 1: pick Gradle, check <a href= "http://www.jetbrains.com/idea/webhelp/importing-project-from-gradle-model.html"> "Use auto-import"</a>, check "Create directories for empty content roots automaticaly", pick JDK version,</p> <p><a href= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_2.png"> <img src= "http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_gradle/image/intellij_idea_create_java_project_with_gradle_2.png" width="900" height="624.324324324324" class="img" /></a> ☝ step 2: specify settings of the project.</p> <p>Alternatively, one can create a Java project using Gradle from UNIX command line and optionally import it later into an IDE such as IntelliJ IDEA or Eclipse.</p> <h2>Create a Java project using Gradle</h2> <p>Create a project using Gradle:</p> <pre><code>$ mkdir gradlehelloworld $ cd gradlehelloworld $ gradle init --type java-library :wrapper :init BUILD SUCCESSFUL Total time: 4.974 secs </code></pre> <p>Alternatively, use <code>gradle buildInit --type java-library</code> to create it.</p> <p>It automatically creates the project structure including directories and template files:</p> <pre><code>$ tree . ├── build.gradle ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main │ └── java │ └── Library.java └── test └── java └── LibraryTest.java 7 directories, 8 files </code></pre> <h2>Make a minimal and slightly non-trivial Java program including tests and Gradle build script</h2> <p>I'll rename <code>Library</code> to <code>Main</code> in the file names and source code, and create a toy Java project with a test:</p> <p><code>Main.java</code>:</p> <pre><code>package gradlehelloworld; import com.google.common.base.Strings; public class Main { public static void main(String[] args) { System.out.println(getGreeting("World")); System.out.println("My name is " + System.getProperty("gradlehelloworld.name")); } static String getGreeting(String name) { return Strings.repeat(String.format("Hello %s!\n", name), 2); } } </code></pre> <p><code>MainTest.java</code>:</p> <pre><code>package gradlehelloworld; import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; import org.junit.Test; public class MainTest { @Test public void getGreeting() { Main classUnderTest = new Main(); assertThat(classUnderTest.getGreeting("World"), equalTo("Hello World!\nHello World!\n")); } } </code></pre> <p>Edit the Gradle build script <code>gradlehelloworld/build.gradle</code>:</p> <pre><code>// Apply the java plugin to add support for Java apply plugin: 'java' apply plugin: 'application' sourceCompatibility = '1.6' mainClassName = 'gradlehelloworld.Main' // In this section you declare where to find the dependencies of your project repositories { // Use 'maven central' for resolving your dependencies. // You can declare any Maven/Ivy/file repository here. mavenCentral() } // In this section you declare the dependencies for your production and test code dependencies { // The production code uses the SLF4J logging API at compile time // compile 'org.slf4j:slf4j-api:1.7.5' compile 'com.google.guava:guava:17.0' // Declare the dependency for your favourite test framework you want to use in your tests. testCompile 'junit:junit:4.11' } run { systemProperty 'gradlehelloworld.name', 'FooBar' } </code></pre> <p>I added these code in <a href= "https://github.com/lumeng/repogit-java-memory-use-experiment">a repository on GitHub</a>. The actual code in the repository is slightly different from the above as I have developed new content and played with variations in it.</p> <h2>Run/build Java project using Gradle</h2> <p>Let Gradle download dependency Guava from Maven Central repository, compile <code>Main.java</code> program, set <code>gradlehelloworld.name</code> to <code>"FooBar"</code>, and run it with Guava on the Java classpath:</p> <pre><code>$ gradle run :compileJava Download http://repo1.maven.org/maven2/com/google/guava/guava/17.0/guava-17.0.pom Download http://repo1.maven.org/maven2/com/google/guava/guava-parent/17.0/guava-parent-17.0.pom Download http://repo1.maven.org/maven2/com/google/guava/guava/17.0/guava-17.0.jar :compileJava FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. &gt; invalid source release: 1.8 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 14.309 secs </code></pre> <p>If I change</p> <pre><code>sourceCompatibility = '1.8' </code></pre> <p>to</p> <pre><code>sourceCompatibility = '1.6' </code></pre> <p>it then works:</p> <pre><code>$ gradle run :compileJava :processResources UP-TO-DATE :classes :run Hello World! Hello World! My name is FooBar BUILD SUCCESSFUL Total time: 6.13 secs </code></pre> <p>To use <code>sourceCompatibility = '1.8'</code>, it might need to set <code>JAVA_HOME</code> environment variable to value of <code>/usr/libexec/java_home -v 1.8</code>.</p> <p>Do a full build including running tests:</p> <pre><code>$ gradle build :compileJava :processResources UP-TO-DATE :classes :jar UP-TO-DATE :assemble UP-TO-DATE :compileTestJava UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test UP-TO-DATE :check UP-TO-DATE :build UP-TO-DATE BUILD SUCCESSFUL Total time: 6.489 secs </code></pre> <p>Check the test report at</p> <pre><code>./build/reports/tests/index.html </code></pre> <p>Remarks:</p> <ul> <li>Note it uses <code>assertThat</code> instead of <code>assertTrue</code></li> </ul> <p><code>assertTrue(classUnderTest.getGreeting("World"), equalTo("Hello World!\nHello World!\n"));</code></p> <p><code>assertThat(classUnderTest.getGreeting("World"), equalTo("Hello World!\nHello World!\n"));</code></p> <ul> <li>Note the use of <code>import static</code> (c.f. <a href= "http://stackoverflow.com/questions/6772647/junit-method-not-found"> a related stackoverflow.com thread</a>.)</li> </ul> <p>At this point your project structure looks like:</p> <pre><code>$ tree . ├── build │ ├── classes │ │ ├── main │ │ │ ├── gradle_hello_world │ │ │ │ └── Main.class │ │ │ └── gradlehelloworld │ │ │ └── Main.class │ │ └── test │ │ └── gradlehelloworld │ │ └── MainTest.class │ ├── dependency-cache │ ├── libs │ │ ├── gradle_hello_world.jar │ │ └── gradlehelloworld.jar │ ├── reports │ │ └── tests │ │ ├── classes │ │ │ ├── css │ │ │ │ ├── base-style.css │ │ │ │ └── style.css │ │ │ ├── gradlehelloworld.MainTest.html │ │ │ ├── htc │ │ │ │ └── css3-pie-1.0beta3.htc │ │ │ └── js │ │ │ └── report.js │ │ ├── css │ │ │ ├── base-style.css │ │ │ └── style.css │ │ ├── htc │ │ │ └── css3-pie-1.0beta3.htc │ │ ├── index.html │ │ ├── js │ │ │ └── report.js │ │ └── packages │ │ ├── css │ │ │ ├── base-style.css │ │ │ └── style.css │ │ ├── gradlehelloworld.html │ │ ├── htc │ │ │ └── css3-pie-1.0beta3.htc │ │ └── js │ │ └── report.js │ ├── test-results │ │ ├── TEST-gradlehelloworld.MainTest.xml │ │ └── binary │ │ └── test │ │ ├── output.bin │ │ ├── output.bin.idx │ │ └── results.bin │ └── tmp │ └── jar │ └── MANIFEST.MF ├── build.gradle ├── build.gradle~ ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle ├── settings.gradle~ └── src ├── main │ └── java │ ├── Main.java │ └── Main.java~ └── test └── java ├── MainTest.java └── MainTest.java~ 34 directories, 37 files </code></pre> <p>The <code>build/</code> sub-directory contains the build, Java doc pages, JUnit test reports, etc.</p> <h2>Import the project into IntelliJ IDEA</h2> <p><a href="http://www.jetbrains.com/idea/">IntelliJ IDEA</a> is a popular IDE for Java. One can import the Gradle project created above to the IDE.</p> <ol> <li>Import the Gradle project into IntelliJ IDEA, by following the documentation instruction <a href= "http://www.jetbrains.com/idea/webhelp/importing-project-from-gradle-model.html"> "Importing Project from Gradle Model"</a>. For project settings, I just chose "Use default gradle wrapper (recommended)", which seems to cause a predefined set of tasks to be created in the Gradle tool window such as "compile", "build", "clean", etc.</li> <li>It turns out that the depended packages need to be separately specified in IntelliJ IDEA in addition to <code>build.gradle</code>. Namely, download the <code>com.google.guava</code> library by following <a href= "http://www.jetbrains.com/idea/webhelp/downloading-libraries-from-maven-repositories.html"> the instruction in documentation</a> -- I picked <code>com.google.guava:guava:17</code> (format is <code>package:artifact:version</code>) to download. (Warning: in IntelliJ IDEA 13.1, I found that one sometimes need to hit the magnifier icon multiple times for it to find a specific artifact from Maven. So don't get confused if it does not find it the first time.)</li> <li>Add the library as a "project library" (by following the automatically prompted window after step 2) or as a global library, by following the documentation instruction <a href= "http://www.jetbrains.com/idea/webhelp/configuring-project-and-global-libraries.html"> "Configuring Project and Global Libraries"</a>.</li> <li>Specify that your Java module depends on the <code>com.google.guava</code> package, following the documentation instruction <a href= "http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html"> "Configuring Module Dependencies and Libraries"</a>;</li> <li>If the IDE still reports "Cannot resolve symbol 'google'" and highlights 'google' as red in <code>import com.google.common.base.Strings</code>, one may try using <code>File | Invalidate Caches / Restart …</code> to force the rebuilding of syntax highlighting in the editor window.</li> <li>One can then open the Gradle tool window, and run the various Gradle tasks such as "compile", "build", and "clean".</li> </ol> <h2>Run Gradle tasks from IntelliJ IDEA Gradle Tool Window</h2> <p>The official <a href= "http://www.jetbrains.com/idea/webhelp/gradle-tool-window.html">IntelliJ IDEA documentation ''Gradle Tool Window''</a> to get an introduction. Hitting <code>build</code> under "All tasks" is equivalent to running <code>gradle build</code> at UNIX command line. It generates <code>/build</code> under project root path. Note JUnit test report is at <code>/build/reports/tests/</code>; Similarly, <code>compileJava</code> compiles Java and <code>javadoc</code> creates Javadoc pages in <code>/build/javadoc/</code>. See <a href= "http://www.gradle.org/docs/current/userguide/java_plugin.html">Gradle documentation</a> for the list of Gradle tasks.</p> <h2>References</h2> <ul> <li><a href="http://spring.io/guides/gs/gradle/">Spring Framework guides ''Building Java Projects with Gradle'': http://spring.io/guides/gs/gradle/</a></li> <li> http://www.gradle.org/docs/1.12/userguide/tutorial_java_projects.html</li> </ul> Installing and configuring Java on Linux http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_java_on_Ubuntu_Linux/ http://meng6net.localhost/computing/installing_and_configuring/installing_and_configuring_java_on_Ubuntu_Linux/ computing configuration documentation draft installation java linux note software tutorial ubuntu 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 Ubuntu Linux 14.04.</p> <p>The default Java packages on Ubuntu Linux <code>openjdk-7-jdk</code> is from OpenJDK. It should be more or less consistent with <a href= "http://docs.oracle.com/javase/specs/">Java Language and Virtual Machine Specifications</a>. However, on macOS computers, I use Oracle's JDKs for developing Java programs, and some softwares I use such as Android Studio warns that using OpenJDK can be problematic and recommends using Oracle's JDK instead. Therefore, I install Oracle's Java as a standard component on my Linux computers as well. 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.</p> <h2>Installing Oracle JDK 7 using ppa</h2> <p>Install dependency:</p> <pre><code>sudo apt-get install python-software-properties </code></pre> <p>Install Oracle JDK using Webup8.org's PPA:</p> <pre><code>sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-installer # or if you want JDK 8: # sudo apt-get install oracle-java8-installer </code></pre> <p>At command line verify <code>java</code> is pointing to Oracle JDK 7,</p> <pre><code>$ which java /usr/bin/java $ ls -la /usr/bin/java lrwxrwxrwx 1 root 22 Jan 24 07:16 /usr/bin/java -&gt; /etc/alternatives/java* $ ls -la /etc/alternatives/java lrwxrwxrwx 1 root 39 Jul 8 12:04 /etc/alternatives/java -&gt; /usr/lib/jvm/java-7-oracle/jre/bin/java* </code></pre> <h2>Installing Oracle JDK 8</h2> <pre><code>$ sudo apt-get install oracle-java8-installer </code></pre> <h2>Choosing default Java</h2> <p>Choose Java 7 as the default for <code>java</code> and <code>javac</code>:</p> <pre><code>$ java -version java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode) $ sudo update-alternatives --config java There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-oracle/jre/bin/java 1073 auto mode 1 /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java 1071 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 1072 manual mode 3 /usr/lib/jvm/java-8-oracle/jre/bin/java 1073 manual mode Press enter to keep the current choice[*], or type selection number: 2 update-alternatives: using /usr/lib/jvm/java-7-oracle/jre/bin/java to provide /usr/bin/java (java) in manual mode $ sudo update-alternatives --config javac There are 3 choices for the alternative javac (providing /usr/bin/javac). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-oracle/bin/javac 1073 auto mode 1 /usr/lib/jvm/java-7-openjdk-amd64/bin/javac 1071 manual mode 2 /usr/lib/jvm/java-7-oracle/bin/javac 1072 manual mode 3 /usr/lib/jvm/java-8-oracle/bin/javac 1073 manual mode Press enter to keep the current choice[*], or type selection number: 2 update-alternatives: using /usr/lib/jvm/java-7-oracle/bin/javac to provide /usr/bin/javac (javac) in manual mode $ java -version java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode) </code></pre> <h2>Setting the <code>JAVA_HOME</code> environment variable</h2> <p>On Ubuntu Linux, system environment variable can be set in <code>/etc/environment</code> by adding the line</p> <pre><code>JAVA_HOME="/usr/lib/jvm/java-7-oracle" </code></pre> <p>Alternatively, set it in <code>$HOME/.bashrc</code> or <code>$HOME/.bash_profile</code></p> <h2>References</h2> <ul> <li> <p><a href= "https://www.digitalocean.com/community/tutorials/how-to-install-java-on-ubuntu-with-apt-get"> digitalocean.com,〈How To Install Java on Ubuntu with Apt-Get〉</a></p> </li> <li> <p> http://askubuntu.com/questions/56104/how-can-i-install-sun-oracles-proprietary-java-jdk-6-7-8-or-jre</p> </li> <li> <p><a href= "http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html"> webup8.org,〈INSTALL ORACLE JAVA 7 IN UBUNTU OR LINUX MINT VIA PPA REPOSITORY〉</a></p> </li> <li> <p><a href= "http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html"> webup8.org,〈INSTALL ORACLE JAVA 8 IN UBUNTU OR LINUX MINT VIA PPA REPOSITORY〉</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> </ul>