How do I use JDK 7 on Mac OSX?

okysabeni picture okysabeni · Jun 7, 2011 · Viewed 205.7k times · Source

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?

Answer

vectorsize picture vectorsize · Oct 21, 2011

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 :)