How do I use JDK 7 on Mac OSX?
Asked Answered
F

16

109

I would like to use the WatchService API as mentioned in this link: http://download.oracle.com/javase/tutorial/essential/io/notification.html

After reading around, I found out that WatchService is part of the NIO class which is scheduled for JDK 7. So, it is in beta form. It's fine.

http://jdk7.java.net/download.html has the JDK which I downloaded and extracted. I got a bunch of folders. I don't know what to do with them.

Then, I read around some more and found that some nice group of people created JDK 7 as a binary so someone like me can install it easily. It is called Open JDK: http://code.google.com/p/openjdk-osx-build/

So, I downloaded the .dmg file and install it. Then I open "Java Preference" and see that OpenJDK7 is available.

So, now I feel that I can start trying out WatchService API. From the tutorial in the first link, the author gave a .java file to test it out first and make sure that it is running. Here is the link to the file: http://download.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java

So, I boot up Eclipse (actually I use STS) and create a new Java project and choose JaveSE-1.7 in the "use an execution environment JRE:". Under the src folder, I copy pasted the WatchDir.java file.

And I still see tons of squiggly red lines. All the "import.java.nio.*" are all red and I cannot run it as a Java app.

What do I need to do?

Firstborn answered 7/6, 2011 at 15:12 Comment(5)
Can you run and compile it on the console?Adornment
You mean: $javac WatchDir.java and then $ Java WatchDir? I will try that. Thanks! Although I would love to have it to be available in Eclipse IDE.Firstborn
From today there is an official Oracle release - Java SE 7u4Demodena
Make sure you are using 64bit Eclipse. Had no luck running 32bit one on 1.7 jvm with any options.Gareri
Update: Java 7 is now at end-of-life. To move on to the long-term support (LTS) versions of Java, see: How to install Java 8 on Mac and Obtain Java 11 for macOS?.Diagnostician
T
62

Oracle has released JDK 7 for OS X.

Thermotensile answered 1/5, 2012 at 11:54 Comment(10)
I will update the answer with the latest information. Please let me know if this is not the right answer. Thanks.Firstborn
@Yko - look at BasilBourque's for the most complete info (or mine for the first Oracle JDK7 version 4)Demodena
Note that in order to make applications use Oracle's Java 1.7 in favor of Apple's Java 1.6, I had to run /Library/Java/JavaVirtualMachines/1.7.0.jdk and drag "Java SE 7" to the top of the list on the "General" tab. This will also make javac point to the Java 1.7 version when running the terminal.Untold
Oracle's JDK7 is not working on Snow Leopard. It says it's supported only Lion.Crabb
@Sangdol - Yes, Oracle's Java implementation runs only on Lion and later. As noted in my answer to this question.Diagnostician
The link seems to be dead, maybe this one will work: docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jdk.htmlAleshia
@Untold The install process seems to be better now. I ran the installer from the dmg under Mountain Lion and I was able to use it straight away in an existing terminal window.Alvarado
@Sangdol I'm on Snow Leopard 10.6.8 and I took the route of installing jdk7 on VirtualBox + Ubuntu. I decided it was easier to do that instead of flips and twists to get it to work natively. My 2c!Faceless
oracle.com/technetwork/java/javase/downloads/…Sabatier
Oracle no longer provides a link to Java 7, at least not without being part of their paid customer support network. I used the Cask solution provided by @abe312 below and was able to install Java 7.Embody
A
87

This is how I got 1.7 to work with Eclipse. I hope it helps.

  1. I Downloaded the latest OpenJDK 1.7 universal (32/64 bits) JDK from Mac OS/X branch from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
  2. copied the jdk to /Library/Java/JavaVirtualMachines/ next to the default 1.6.0 one
  3. In Eclipse > Preferences > Java > Installed JREs you add a new one, of type MacOS X VM, and set the home as /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home and name Java SE 7 (OpenJDK)
  4. Click Finish
  5. Set the added JRE as default

that should be it :)

Alvinaalvine answered 20/10, 2011 at 22:22 Comment(6)
thanks my elcipse completely hung.. so i tried to use sdk 1.7 worked perfectly! :=)Mithgarthr
That project (openjdk-osx-build) has shut down and no longer offers any downloads.Impolicy
step 2 can be just: run downladed jdk-7u40-macosx-x64.dmg and it will automatically add file to the pathBrede
When adding an installed JRE to Eclipse, Eclipse can also "search" for installed JREs. That's how I found my existing installation of 1.7 JDK.Groundwork
Note that the oracle jdk 1.7 installer will put the VM into /Library/Java/JavaVirtualMachines/ for you (march 2014)Judd
java.io.nullPointerException access denied errors are also solved by this, for any searchwords.Physiology
T
62

Oracle has released JDK 7 for OS X.

Thermotensile answered 1/5, 2012 at 11:54 Comment(10)
I will update the answer with the latest information. Please let me know if this is not the right answer. Thanks.Firstborn
@Yko - look at BasilBourque's for the most complete info (or mine for the first Oracle JDK7 version 4)Demodena
Note that in order to make applications use Oracle's Java 1.7 in favor of Apple's Java 1.6, I had to run /Library/Java/JavaVirtualMachines/1.7.0.jdk and drag "Java SE 7" to the top of the list on the "General" tab. This will also make javac point to the Java 1.7 version when running the terminal.Untold
Oracle's JDK7 is not working on Snow Leopard. It says it's supported only Lion.Crabb
@Sangdol - Yes, Oracle's Java implementation runs only on Lion and later. As noted in my answer to this question.Diagnostician
The link seems to be dead, maybe this one will work: docs.oracle.com/javase/7/docs/webnotes/install/mac/mac-jdk.htmlAleshia
@Untold The install process seems to be better now. I ran the installer from the dmg under Mountain Lion and I was able to use it straight away in an existing terminal window.Alvarado
@Sangdol I'm on Snow Leopard 10.6.8 and I took the route of installing jdk7 on VirtualBox + Ubuntu. I decided it was easier to do that instead of flips and twists to get it to work natively. My 2c!Faceless
oracle.com/technetwork/java/javase/downloads/…Sabatier
Oracle no longer provides a link to Java 7, at least not without being part of their paid customer support network. I used the Cask solution provided by @abe312 below and was able to install Java 7.Embody
D
56

Java 9, 10, 11

Years ago, Apple joined the OpenJDK project, transferring their formerly proprietary macOS-specific JVM code as free-of-cost open-source. Apple ceased distribution of their own branded JVM/JDK, relying on Oracle’s branded releases to include a version for macOS.

Oracle has recently announced their intention to bring their Oracle-branded JVM release to feature parity with the OpenJDK project, with virtually the same code base. The company even donated their previously commercial tools, Flight Recorder & Mission Control, to the OpenJDK project. This is part of the shift to a new rapid “release train” plan for predictably scheduled versioning of Java and OpenJDK. Notably, the periods for free-of-cost public updates is now shortened. See this 2017-09 announcement and this posting by Mark Reinhold.

As a result of all this, macOS users of Java have a choice of vendors for a Java implementation. At this point, at least three sources are based on OpenJDK for macOS:

  • Oracle releases of the JDK and JRE, with optional paid support.
  • Azul Systems releasing:
    • Zulu line of free-of-cost JVMs with optional paid support.
    • Zing line of commercial JVMs with special features such as an alternate garbage-collector.
  • OpenJDK source code, roll-your-own compilation & installation (perhaps not practical for most of us).

Meanwhile, IBM donated code for a JVM to the Eclipse Foundation, now housed in the OpenJ9 project. I wonder if they might support a macOS release as well, though it is too soon to tell.

Personally, I am currently using the Zulu release of Java 10.0.1 from Azul on macOS High Sierra successfully with IntelliJ 2018.2 to produce Java-backed web apps with Vaadin.

Installation/Removal

Both Oracle and Azul provide utterly easy-to-use installers to install the JVM/JDK on your Mac. Verify your installation by using the Terminal.app (or equivalent) to type and run:

java -version

You will find the Java installations in this folder at the root level of your drive (not in your home folder):

/Library/Java/JavaVirtualMachines

Each version from each vendor is found there, in a labeled nested folder. You can delete any installation simply by deleting the nested folder for that version and providing your system password when prompted.

Java 8

You can download the Java Development Kit (JDK) for Java 8 for the supported versions of Mac OS X:

  • Mountain Lion (10.8.3+)
  • Mavericks (10.9)
  • Yosemite (10.10)
  • El Capitan (10.11)

Each version of JVM you install can be found here:

/Library/Java/JavaVirtualMachines

For more instructions and FAQ, see this Oracle Guide.

Java 7

For every release of Java 7 since Update 4, a Mac version has been ready alongside the other platforms. Runs on Macs with 64-bit hardware on Lion (10.7.3+), Mountain Lion (10.8.3+), and Mavericks (10.9.x).

Oracle announced the official release of the JDK for Java SE 7 Update 4 on Mac OS X (Lion), as of 2012-04-26. No more need for the tricks discussed on this page.

Installation is simple per these instructions:

  1. Download from the usual place on the Oracle web site.
  2. Mount the DMG.
  3. Run the installer.

This release has a few limitations, most notably the lack of support for Java Web Start and the Java Plugin for web browsers. That support is expected later this year.

After installing, read the JDK for Mac ReadMe. Most importantly, if you want Java 7 to be the default, drag it to the top of the list in the Java Preferences app found in your Utilities folder.

Mac OS X easily supports multiple JVMs simultaneously. Each is now found here:

/Library/Java/JavaVirtualMachines

Congratulations to the Apple & Oracle teams for their achievement. This geek gets a thrill seeing Mac OS X listed as a "Certified System Configuration".

Tip: To start Eclipse on a Mac with only Java 7 installed, open the alias file named eclipse rather than the file named Eclipse.app.

Java 6

Apple continues to supply an up-to-date implementation of Java 6 for all versions of Mac OS X up through Mountain Lion.

If you do something that requires Java, such as type "java -version" in Terminal.app, a dialog appears offering to install Java for you. If you accept, installation happens automatically similar to other "Software Updates" from Apple.

You will find Java installed in this location, different than Java 7 & 8:

/System/Library/Java/JavaVirtualMachines

Java 6 has reached end-of-life with Oracle as of 2013-02 (unless you have a commercial support agreement with Oracle). So you should be moving to Java 7 or 8.

Testing New Version

In Terminal.app, type java -version to verify which version is the current default.

Deleting Old Versions

After installing a fresh version, you may want to visit the folder described above to delete old versions. Move the folder to the Trash, and provide your System password complete the move.


By the way, Apple provides a mailing list for developers’ technical issues related to Java on OS X.

Diagnostician answered 27/4, 2012 at 21:40 Comment(9)
Thanks, this worked great for me. Need to change it in preferences too.Nyhagen
@Tony: Yes, changing your Preferences is mentioned in 4th paragraph.Diagnostician
Doesn't work. I run the installer and it reports "Success", but nothing seems to actually get installed. /System/Library/Java/JavaVirtualMachines only contains Java 6 JDK.Impolicy
@Impolicy - (a) Look in /Library/Java/JavaVirtualMachines (not /System). (b) Did you read the Oracle's installation docs, the ReadMe, and set Preferences as described in my answer above?Diagnostician
@BasilBourque - (a) Yep, it wasn't there either. (b) Yes; I do not have/see a 'Java Preferences' app in Application/Utilities. But in any case, I found the issue. I was trying to install the Java 7 package off of java.com (~50MB). When I went and got the one from Oracle (~150MB), that one installed correctly. Still no 'Java Preferences' app though.Impolicy
With Java 7 r21, I see /Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/. The Java Control Panel is now accessed through System Preferences (OSX 10.8.3)Traveler
I suggest clarifying in the post that /Library/Java/JavaVirtualMachines is a new location, not to be confused with /System/Library/Java/JavaVirtualMachines where 1.6 JDK was installed. I did not catch that until reading through the comments.Wrangler
@Impolicy Yes, I've seen some strange behavior with the different versions of Oracle's installers. I've not documented it, but it seems like the install location varies. I always have to hunt around to find and delete old versions. Using Java's own updater feature almost never works for me, so I manually delete old JVM and download the latest installer to do a fresh install.Diagnostician
Thank you. :-) The path /Library/Java/JavaVirtualMachines you mention gave me the hint so now I have a working jre.Practiced
A
17

An easy way to install Java 7 on a Mac is by using Homebrew, thanks to the Homebrew Cask plugin (which is now installed by default).

Run this command to install Java 7:

brew cask install caskroom/versions/java7
Amphitryon answered 9/11, 2015 at 23:36 Comment(4)
"Error: No available cask for caskroom/versions/java7"Orcus
@Orcus Maybe your caskroom/versions is out of date? It's definitely here: github.com/caskroom/homebrew-versions/blob/master/Casks/…Amphitryon
@Orcus Try running brew tap caskroom/versions before running the install commandAmphitryon
@Orcus If that doesn't work, consult the cask documentation, or file an issue: github.com/caskroom/homebrew-caskAmphitryon
D
14

Get cask

brew tap caskroom/cask

Install java7:

brew tap caskroom/versions
brew cask install java7

(I had difficulty finding the download link of java7 on oracle website, as they're just "recommending" java8 )

EDIT January 2018 (As pointed by Ankur):

Quote from GitHub by abigailbunyan:

[U]se zulu7 cask. Zulu is a certified build of OpenJDK produced by Azul Systems that should be around for a long time (they even offer JDK6 builds still).

brew cask install caskroom/versions/zulu7
Diamante answered 29/12, 2016 at 14:20 Comment(2)
The accepted answer for this question is outdated. This seems to be the best way to get Java 7 installed these days.Embody
this cask was removed (see: github.com/caskroom/homebrew-versions/pull/3914). the suggested alternative is to use zulu7.Chenoweth
W
9

I know that some may want to smack me for re-opening old post, but if you feel so do it I just hope this may help someone else trying to set JDK 7 on Mac OS (using IntelliJ).

What I did to get this working on my machine is to:

  • followed instructions on Oracle JDK7 Mac OS X Port for general installation
  • in IntelliJ open/create new project so you can add new SDK (File > Project Structure)
  • select Platform Settings > SDKs, press "+" (plus) sign to add new SDK
  • select JSDK and navigate to /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home. Do not get it mistaken with /Users/YOUR_USERNAME/Library/Java/. This will link 4 JARs from "lib" directory (dt.jar, jconsole.jar, sa-jdi.jar and tools.jar)
  • you will need also add JARs from /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home/jre/lib (charsets.jar, jce.jar, JObjC.jar, jsse.jar, management-agent.jar, resources.jar and rt.jar)
Wholly answered 8/11, 2011 at 11:0 Comment(0)
P
7

after installing the 1.7jdk from oracle, i changed my bash scripts to add:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home

and then running java -version showed the right version.

Parnassian answered 15/2, 2013 at 21:44 Comment(0)
O
6

It's possible that you still need to add the JDK into Eclipse (STS). Just because the JDK is on the system doesn't mean Eclipse knows where to find it.

Go to Preferences > Java > Installed JREs

If there is not an entry for the 1.7 JDK, add it. You'll have to point Eclipse to where you installed your 1.7 JDK.

If Eclipse can't find a JRE that is 1.7 compatible, I'm guessing that it just uses your default JRE, and that's probably still pointing at Java 1.6, which would be causing your red squiggly lines.

Overton answered 7/6, 2011 at 15:37 Comment(3)
Good Call! But I need a little bit further guidance. I go under Installed JREs -> Add and point to .../jdk1.7.0/jre and it found a bunch of JAR files. I click Finish. Happily tries to build but now it says .../jdk1.7.0/jre/bin/java: cannot execute binary file. What does that mean?Firstborn
I'm not really sure. You could try pointing eclipse at the base jdk directory instead of the jre directory. I think that's how I've usually done it on Windows, though I haven't ever switched off the default JDK on my Mac. If that doesn't work, you could try installing a different distribution of JDK 7 - there were a few different ones available at the openjdk-osx-build site, maybe you grabbed one that isn't compatible with your system?Overton
I have tried that. It gives me the error of "Target is not a JDK root. System library was not found"Firstborn
D
4

I needed to adapt @abe312's answer since there has been some changes with brew lately.

I installed zulu7 and setup JAVA_HOME by running:

brew install --cask homebrew/cask-versions/zulu7
echo "export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-7.jdk/Contents/Home" >> ~/.zshrc

I had to enter my password for installing zulu7. You may need to modify the last command if you are using a different shell.

Diallage answered 5/1, 2021 at 21:26 Comment(0)
K
2

How about Netbeans, here is an article how to set it up with NB7:

http://netbeanside61.blogspot.com/2011/06/downloading-openjdk7-binary-for-mac-os.html

Maybe similar steps for Eclipse.

Kohinoor answered 25/7, 2011 at 6:15 Comment(1)
I have used netbeans in the past and i will give it a try. Thanks for the link.Firstborn
D
2

As of April 27th there is an offical Oracle release of Java SE 7u4. Download the disk image and run the installer - then see the Mac readme.

Demodena answered 27/4, 2012 at 21:32 Comment(0)
N
2

As of December 2017, previously posted links don't work, but JDK 7 can still be downloaded from Oracle Archives (login required):

http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html

Nippur answered 6/12, 2017 at 18:33 Comment(0)
H
1

The instructions by peter_budo worked perfectly. I had to add the jars under /Library/Java/JavaVirtualMachines/JDK 1.7.0 Developer Preview.jdk/Contents/Home/jre/lib/ to my IntelliJ project libraries. Now it works like a charm. Note that I didn't need my IDE itself to run under 1.7; rather, I only needed to be able to compile and run against 1.7. I'll most likely continue to use Apple's JRE for running the IDE since it's probably more stable with respect to graphics routines (Swing, AWT). Like the OP, I was really keen on testing out the new NIO2 API. Looking good so far. Thanks, Peter.

Hathor answered 11/12, 2011 at 22:43 Comment(0)
C
1

What worked for me on Lion was installing the JDK7_u17 from Oracle, then editing ~/.bash_profile to include: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_13.jdk/Contents/Home

Cislunar answered 8/3, 2013 at 13:16 Comment(0)
S
1

Now, Use command

Update 2020: 04

To install Java7 with homebrew run:

brew tap homebrew/cask-versions
brew cask install java7

Hope this help.

Striker answered 28/4, 2020 at 9:38 Comment(0)
M
0

I updated to Yosemite and Android Studio wouldn't clean my projects or Run them on virtual or real device because of the following error:

Failed to complete Gradle execution.Cause:Supplied javaHome is not a valid folder. You supplied: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

After some research and trouble shooting, I found that the JDK file that was being pointed to at "/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home" wasn't there; all of "JavaVirtualMachines/1.6.0.jdk/Contents/Home" was missing from "/System/Library/Java". So, I copied "JavaVirtualMachines/1.6.0.jdk/Contents/Home" over from "/Library/Java/" to "/System/Library/Java/" and cha ching! I was back in business.

Massie answered 12/9, 2015 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.