home

[Eclipse Tip] How to use Scrapbook pages in Eclipse?

October 1, 2009 · 8 comments

When ever I code a complex logic in Java, I often wants to test a part of that complex code to check my logic correctness or to evaluate a code snippet. I usually do this, by creating a sample class named SandBox with a main method and placing whatever code that I wanted to test inside the main method. Then run the SandBox class. This solved my problem most of the times. But recently, I came to know that Eclipse has a elegant way of doing this in the name of Scrapbook Page.

So, what is Scrapbook Page? To put it simply, it’s just a file where you can place your Java code snippet and execute it directly from the Scrapbook. No need to create any temperory classes (like I did in the previous para). You can even put one line of Java statement like System.out.println(“Hello”) in the Scrapbook and execute it. You’ll get the ‘Hello’ printed on your console. Cool, Isn’t it?

That said, I guess now you know the potential of Scrapbook. Let’s see how to use Scrapbook. To add a scrapbook to your project, go to File -> New -> Other -> Java -> Java Run/Debug -> Scrapbook page. This will add a new file with an extension “.jpage”. In this file you can place any Java statements and you can select the Java statements, right click and either choose to:

  • Inspect - which will show the return value of your statements in an popup box.
  • Display - which will insert the return value of your statements to the Scrapbook itself.
  • Execute - which will run your selected Java statements.

Apart from running Java statements, Scrapbook pages supports content assist also. So, when you type your code in Scrapbook, you’ll get the Autosuggestions as well.

i hope, this information helps you to use Eclipse effectively for your Java development. Do let me know your thought’s about this article.

Related Posts

{ 8 comments… read them below or add one }

IBBoard October 1, 2009 at 6:05 PM

“When ever I code a complex logic in Java, I often wants to test a part of that complex code to check my logic correctness.”
Isn’t that what unit tests are for – testing your logic for correctness? Also, complex logic can often be broken down in to simpler logic, which would remove the problem in the first place :)

Reply

Veera October 1, 2009 at 6:32 PM

@IBBoard

Unit test are entirely different from this Scrapbook pages. When you want to test the complete class for the functionality, Unit tests are helpful.

But what I wanted to say is when only the portion of the code that needs to be evaluated, Scrapbook pages are handy. :)

Reply

ppow October 1, 2009 at 8:58 PM

I agree with IBBoard: whenever you have a non-trivial part of code that you might want to check for correctness you should encapsulate it and create an unit test for it.

Having said that I see the usefulness of scrapbook: its a great place for experimenting and checking/evaluating the possible solutions

Reply

Veera October 1, 2009 at 9:41 PM

@ppow

Agree on that. Scrapbook pages are good for evaluating a piece of code.

Reply

steven October 2, 2009 at 10:12 AM

wow, handy stuff. apparently this has been around for a while

Reply

Veera October 2, 2009 at 11:20 AM

@ Steven

Yes. Scrapbook page feature has been there in Eclipse for a long time. But relatively very few developers know about this handy stuff.

Reply

IBBoard October 2, 2009 at 11:18 PM

Yes and no on the “only testing a portion”. It depends how you use unit tests – i.e. whether they’re part of a decreed process that is added on afterwards, or part of a structured Test Driven Development (which I try to do but sometimes slip up with).

Unit tests can still test little snippets of logic, since you should be breaking your code up in to small chunks. Also, if you use a unit test then it is recorded for future use as well, and any breaking changes you make to your logic will become obvious. ‘Uncle’ Bob Martin’s “Clean Code” book[1] even has (IIRC) a section on explorative unit testing and how it can be useful.

If you’ve not got a clue what your logic is and how it should work then unit tests can also be the building structure to create the logic. It’s all well and good knowing that your logic does what you think it should, but is what you think it should do actually everything it needs to do ;) Without some comparison to known values then you can’t know.

[1] http://www.amazon.co.uk/gp/product/0132350882?ie=UTF8&tag=hiveworldterr-21&linkCode=as2&camp=1634&creative=19450&creativeASIN=0132350882

Reply

IBBoard November 13, 2009 at 11:03 PM

“When ever I code a complex logic in Java, I often wants to test a part of that complex code to check my logic correctness.”
Isn't that what unit tests are for – testing your logic for correctness? Also, complex logic can often be broken down in to simpler logic, which would remove the problem in the first place :)

Reply

Leave a Comment

Previous post:

Next post: