geeky

Eclipse run java code easily

Eclipse has a scrapbook function that allows you to run Java code on the fly. Sometimes you just want to test out a simple piece of code without having to create another Java class, execute it in the main method or create a junit to test it out.

For example, today I wanted to make sure I remember integer division in Java correctly. So I simply just want to execute the following piece of code.

int a = 10;
int b = 8;
int c = 17;
System.out.println(a / b);
System.out.println(c / b);

In order to do that, create a new Scrapbook page in your project.

  • Right click project -> New -> Other… (or CTRL+N)
  • Search “scrapbook”
  • Create a new scrapbook page. I just called mine blah.jpage.
  • Then paste the code in there. Highlight the code, right click and select execute or ctrl+U. The results of your execution will then show up in the console. That comes in pretty handy sometimes.

Eclipse Scrapbook

One thought on “Eclipse run java code easily

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s