1.10. Running Your Code

Problem

How do you run Java code from Eclipse?

Solution

Select Run Run As. In the list that appears, select one of the following items: Java Applet, Java Application, JUnit Test, or Run-time Workbench.

Discussion

To run the code developed over the previous few recipes, select Run Run As Java Application (Eclipse will prompt you to save the file if you haven’t already done so). Figure 1-13 shows the results, with the output of our code, Stay cool., appearing in the Console view at the bottom.

Tip

Before running a Java program for the first time, it’s a good idea to check if Eclipse is using the JRE or JDK you want it to use. By default, Eclipse locates an installed JRE or JDK, but it might not be the one you want to use (for example, Eclipse might have located the JRE installed for a browser you’re using). To check which JRE or JDK Eclipse is using, select Window Preferences to open the workbench preferences. Select Java Installed JREs in the tree pane on the left to display the Installed Java Runtime Environments preference page. The JRE or JDK Eclipse is using is checked; you can check other JREs or JDKs to make Eclipse use them instead. We’ll discuss adding other JREs and JDKs in the next chapter.

Running Java code

Figure 1-13. Running Java code

And that’s it—you’ve developed and run your first Java example.

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.