Oct 6, 2011

Eclipse Service Release Adds Support for Java7

Just last week the Eclipse group released a service release to their Indigo version (v3.7). One notable addition is support for Java v7.

Prior to this release, you could add a Java v7 runtime, but none of the code tools would recognize 1.7 as an acceptable level. None of the new language additions were supported, and it generally became frustrating to try to experiment with Java 7. Of course, it would allow you to compile old code with Java 7, and get Java 7 classes out of the process, but not with Java 7 language updates.

Java v7 Required

In order for Eclipse to use the new Java version, Java v7 needs to be installed. Eclipse will run with a Java Runtime Edition, but I recommend grabbing the JDK when trying to develop software. Visit the Oracle Java download page, select the JDK, and when redirected, accept the agreement and download the version appropriate for your computer. Run the installer or unpack, as makes sense.

For Mac users, you can visit the openjdk-osx-build project hosted on Google Code; it will direct you to the Apple preview project as well as give you updated builds of the reference project. At least until Apple decides that it’ll offer JDK7 in the app store or via their developer’s site.

Downloading Update

Visiting the Eclipse download page should offer the latest Indigo package, labeled SR1. Download and expand the appropriate archive for your OS. Either edit the eclipse.ini file within to add the necessary lines to point to the JDK7 VM, or start with the JDK7 as the first Java in your path for the easiest integration. Of course, you can also add the runtime to the workspace properties after starting Eclipse.

Adding the JDK to the eclipse.ini file has the added benefit of running Eclipse with the JDK. Even if the JDK is first in your path, Eclipse will run using the JRE. Find the eclipse.ini file in the root of your newly expanded Eclipse folder (for Mac users it’s in the Eclipse folder’s Eclipse.app/Contents/MacOS folder). Edit the file with your favorite text editor, and before the line that says -product add the lines below, modifying for your JDK installation path.


Quite technically, the VM just needs to be named before the -vmargs argument. The path should be absolute, not relative, and if running on Windows, should include the drive letter (c:/path/to/jdk) and the slashes should be “reversed” so that nothing is considered an escaped character. The path should end at the folder containing the JDK, not the included JRE. It should be the folder containing the “bin” and “jre” and “lib” folders (at a minimum).

For Mac users, be careful because the JDK is buried in the Library (either system or user) folder; for example, I’ve installed the JDK7 in my home folder, so it’s path is ~/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home due to Mac’s app packaging.

Upgrading Existing Eclipse

One downside to adding a new Eclipse runtime is that you need to re-add any plug-ins you use. If you’re already using a pretty recent version of Eclipse (I know it works with Indigo, and I think it works with Helios, but I’ve not tested that), the service release is available as an update via Eclipse’s built-in tools, though.

To upgrade is pretty straight-forward, and the only troubles I’ve had are bandwidth related (if you get connected to a slow or busy mirror, you have to wait longer for the download…). Start Eclipse (I generally use an empty workspace to avoid fouling any existing projects) and hit the “Check for Updates” item in the Help menu. It should spin for a little bit and find that the 3.7 SR1 is available, as well as any updates for plug-ins you’ve installed via the marketplace or an update site supported by the Eclipse installer (I’m unsure how manually added packages work; I can find all of my plug-ins via URL or in the marketplace). It will download and install in place over your existing Eclipse, and upon restarting you’ll be running the Java7-friendly version.

After upgrading Eclipse, affect your path, the eclipse.ini, or add the JDK to your workspace settings as discussed above.

Using Eclipse with JDK7

Once you’ve got Eclipse running with the update, and JDK7 is installed as a runtime, it’s time to grin some Java 7 code. First we need to tell Eclipse we’re using Java7 in our project.

In an existing project, visit the project’s properties page and change the Java version to use Java 7. This can be done myriad ways. If you’re using a faceted project, first visit the Project Facets item in the properties panel, and change the Java version to 1.7. This should change the Java compiler settings as well, but double-check the Java Compiler item to make sure the JDK compliance is set to 1.7 (this might not change if you’ve explicitly changed it before). Finally, check the Java Build Path and make sure that the Java Runtime is your JDK7.  If you’re making a web application, you’ll need to be sure your application or Servlet server (like Tomcat) is also using Java7 in its launch configuration.

If you’re making a new project, choose the JDK7 as your runtime. If you’re using project facets, make sure the Java version is 1.7. Likewise, if you’re making a web application, make sure your server’s configured runtime is using Java 7.

Now add or modify a class and put some Java 7 goodness in it. What to do? A very easy test is to check out the number formatting. Change or add a simple number assignment by putting underscores in the number, such as the following.

int i = 1_0;

If the IDE doesn’t bark, you’ve got JDK7 correctly configured.

For a more intense test, find a try/catch block that has more than one caught Exception; this is especially helpful if the catch block does the same thing with each Exception it catches. The following truncated code shows how you can take advantage of Java 7’s multiple Exception blocks:

} catch (NullPointerException e) {
    logger.error("Exception!", e);
} catch (IllegalArgumentException e) {
    logger.error("Exception!", e);

Can become:

} catch(NullPointerException | IllegalArgumentException e) {
    logger.error("Exception!", e);

Finally, try reducing some large if/else block where strings are compared to use the String switch statement:

else if("two".equals(string))

Can become:

    case "one": one(); break;
    case "two": two(); break;
    default: other();

In a very short example, that doesn’t seem helpful, but there are lots of cases where that makes more sense.

About the Author

Object Partners profile.
Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]