1.11. Running Code Snippets
Problem
You want to test just a section of code, without having to get a whole program to run.
Solution
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.
Discussion
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.

Figure 1-14. Using a scrapbook page
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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access