Select Which Version of Java to Use with Eclipse

By Barry Burd

Eclipse normally looks on the computer for Java installations and selects an installed version of Java to use for running your Java programs. The computer may have more than one version of Java, so double-check Eclipse’s Java version selection.

These steps are optional. Follow them only if you suspect that Eclipse isn’t using your computer’s favorite version of Java.

  1. In Windows: From the Eclipse main menu, choose Window→Preferences. On the Mac: From the Eclipse main menu, choose Eclipse→Preferences.

    As a result, the Eclipse Preferences dialog box appears. (You can follow along in the figure.)

    image0.jpg

  2. In the tree on the left side of the Preferences dialog box, expand the Java branch.

  3. Within the Java branch, select the Installed JREs subbranch.

  4. Look at the list of Java versions (Installed JREs) in the main body of the Preferences dialog box.

    In the list, each version of Java has a check box. Eclipse uses the version whose box is checked. If the checked version isn’t your preferred version (for example, if it isn’t version 6 or later), you have to make changes.

  5. If your preferred version of Java appears in the Installed JREs list, select that version’s check box.

  6. If your preferred version of Java doesn’t appear in the Installed JREs list, click the Add button.

    When you click the Add button, the JRE Type dialog box appears, as shown in the figure.

    image1.jpg

  7. In the JRE Type dialog box, double-click Standard VM.

    As a result, the JRE Definition dialog box appears, as shown in the figure. What you do next depends on a few different factors.

    image2.jpg

  8. Fill in the JRE Home field in the dialog box.

    How you do this depends on the operating system:

    • In Windows: Browse to the directory in which you’ve installed your preferred Java version. The directory is likely either C:Program FilesJavajre7, C:Program FilesJavajdk1.7.0, C:Program Files (x86)Javajre8, or something of that sort.

    • On the Mac: Use the Finder to browse to the directory in which you’ve installed your preferred Java version. Type the name of the directory in the dialog box’s JRE home field.

      Directories such as /System and /Library don’t normally appear in the Mac’s Finder window. To browse to one of these directories (to the /Library directory, for example) choose Go→Go to Folder on the Finder’s menu bar. In the resulting dialog box, type /Library and then press Go.

      As you navigate toward the directory containing your preferred Java version, you might encounter a JDK 1.7.0.jdk icon, or another item whose extension is .jdk. To see the contents of this item, control-click the item’s icon and then select Show Package Contents.

    You might have one more thing to do back in the JRE Definition dialog box.

  9. Look at the JRE Name field in the JRE Definition dialog box; if Eclipse hasn’t filled in a name automatically, type a name (almost any text) in the JRE Name field.

  10. Dismiss the JRE Definition dialog box by clicking Finish.

    The Preferences dialog box in Eclipse returns to the foreground. Its Installed JREs list contains the newly added version of Java.

  11. Select the check box next to the newly added version of Java.

    You’re almost done. (You have a few more steps to follow.)

  12. Within the Java branch on the left side of the Preferences dialog box, select the Compiler subbranch.

    In the main body of the Preferences dialog box, you see the Compiler Compliance Level drop-down list, as shown in this figure.

    image3.jpg

  13. In the Compiler Compliance Level drop-down list, select 1.5 or 1.6.

    Android works with only Java 1.5 or 1.6.

  14. Whew! Click the Preferences dialog box’s OK button to return to the Eclipse workbench.