There’s another convenient way to run
Java code in
Eclipse: you can use a scrapbook page
. Scrapbook
pages enable you to execute code, even partial programs, on the fly.
Using scrapbook pages isn’t an essential skill in
Eclipse, but it’s a useful thing to know.
After you’ve created a scrapbook page, you can copy
and paste code to that page and run it. Create a scrapbook page by
selecting File→ New→ Scrapbook Page. Enter a name
for the page, such as ScrapPage
, in the File box,
and click Finish. The new scrapbook page is stored in the Package
Explorer as ScrapPage.jpage
, as shown in Figure 1-14.
Enter the code you want to run in this new page. For instance, to run
the example code in the FirstApp
project we just
ran, enter the following code in the scrapbook page; note that to
reach the main
method of our example, you have to
qualify its name with the name of the package it’s
in:
String[] args = {};
org.cookbook.ch01.FirstApp.main(args);
You select the code you want to run in the scrapbook page by highlighting it. In this case, select all the code in the scrapbook page, right-click it, and click Execute, or select Run→ Execute. (If you need imports for the code in the scrapbook page, right-click the scrapbook’s code, and select Set Imports in the context menu.)
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.