Rubit
Rubit

Reputation: 49

Is there anyway to run only selected code in Java Eclipse?

Is there anyway that I can run only part of the code which I select, instead of Ctrl + F11 and execute all?

Upvotes: 3

Views: 5798

Answers (5)

JaiRamen
JaiRamen

Reputation: 1

You can absolutely run part of your code (in Eclipse). Just use Shift + click to select the parts you do not want to run, and press Ctrl + / to comment out huge bits of code quickly.

To uncomment, make sure you select continuous sections of vertical //'s, and then use the same Ctrl + / command to quickly get rid of all the //'s.

Note: if you press Shift + Ctrl + / with code selected, you will get the "block comment" (`/* code not running */"-type). Use the same keys to undo it, Shift + Ctrl + /.

For me, it is less easy to locate those quickly versus the massive vertical line of //'s

Upvotes: 0

Rodrigo Eggea
Rodrigo Eggea

Reputation: 124

In Eclipse you can create a "Scrapbook Page" to test pieces of codes.

In Eclipse go to:

File -> New -> Scrapbook Page -> "MyScrapbook" 

Eclipse will create a file "MyScrapbook.jpage"

Write the code in the file, don't create class or main.

Select the piece of code you have wrote and want to run, and press CTRL+U

Upvotes: 1

FirstTime
FirstTime

Reputation: 11

You can easily test a part of your code through a junit test.

  • get JUnit
  • add the annotation @Test on the method you want to test
  • left click on the method => debug as... => JUnit test

You probably need to mock the database calls

Upvotes: 1

Louis F.
Louis F.

Reputation: 2048

If you just want to test a method without any context, you could try using the groovy console and copy / paste there the code you want to "test". You will be able to run it and "test" it.

A link to it : http://www.vogella.com/tutorials/Groovy/article.html

Otherwise the only way to do it is to go through traditional debug mode.

Hope it helps !

Upvotes: 0

Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

No. There is no way to run only part of the code.

Upvotes: 3

Related Questions