Recipe 1.11 Running Code Snippets

     

1.11.1 Problem

You want to test just a section of code, without having to get a whole program to run.

1.11.2 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.

1.11.3 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
figs/ecb_0114.gif

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.)

Execute runs the code in a scrapbook page, but another option also exists. If you select Display instead, the net return value of the code you've selected appears in the scrapbook. This technique is useful if you don't want to stud your code with println statements.




Eclipse Cookbook
Inside XML (Inside (New Riders))
ISBN: 596007108
EAN: 2147483647
Year: 2006
Pages: 232
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net