Run/debug a class in Eclipse as a JUnit test or Java app with a single keystroke

If you’ve used Eclipse for a while, you may have run into the commands Run As and Debug As. These are used to launch a class/project as a particular application, eg. a Java application, JUnit test or Eclipse application.

They’re normally accessed with the mouse via the right-click menu or the pulldown menu on the run/debug icons in the toolbar. However, using the mouse to launch these classes isn’t the fastest way of doing things, especially if you’re frequently running different classes, such as JUnit tests.

To help with this, Eclipse has keyboard shortcuts to invoke the Run As/Debug As commands. They really speed up launching apps (which you’ll do often in a day’s work). I’ll show you these shortcuts and also a couple of others to make life easier.

How to Run As/Debug As with the keyboard

Here are the keyboard shortcuts for a couple of the more popular launchers. You can, of course, configure any of these keys to make them easier by going to Window > Preference > General > Keys, especially if it’s a launcher you use often.

Run As
Debug As
Alt+Shift+X, J Alt+Shift+D, J Run/debug as a Java application. Produces an error message if the class doesn’t contain a static main method.
Alt+Shift+X, T Alt+Shift+D, T Run/debug as a JUnit test. Produces an error message if the class doesn’t extend TestCase or doesn’t include JUnit 4 @Test annotations.
Alt+Shift+X, Q Alt+Shift+D, Q Run/debug as an Ant build. Produces an error message if the file isn’t valid Ant XML.
Alt+Shift+X, E Alt+Shift+D, E Run/debug as an Eclipse application. This spawns another Eclipse instance and deploys your development plugins to that instance so you can test your plugins. Runs an Eclipse RCP application if you have one defined.
Alt+Shift+X, P Alt+Shift+D, P Run/debug as an Eclipse JUnit Plugin test. Same as JUnit, but runs the test under the OSGi framework as a bundle. Used to test Eclipse applications and plugins.

The list is not exhaustive as I’ve only listed the more popular ones and you may have plugins that contribute more launchers. You can get a full list of the available ones by pressing and releasing Alt+Shift+X. After a second, a popup will appear in the bottom right corner with the list of available options. Here’s an example of what you might see:

Alternatively go to Windows > Preferences > General > Keys and search for “Run” – you’ll get a more comprehensive list, even for ones without existing key bindings.

Eclipse is also nice enough to show you the shortcuts on the menu items, just in case you forget them.

Relaunching the last launched application

So now that you know how to launch the class initially, you may want to rerun this class a couple of times before you’re done. Using a shortcut is much faster than reaching for the mouse to click the toolbar icon, so here are the shortcuts to use:

F11 Launch the last launched application in debug mode (ie. allow breakpoints, hotswapping of code, etc).
Ctrl+F11 Launch the last launched application, but not in debug mode. Breakpoints will be skipped and there’ll be no Hotswapping.
Ctrl+Shift+F11 Launch the last launched external tool, eg. a command line app, external Ant file, etc. Only useful if you have configured external tools via Run > External Tools or some other way.

Related Tips

Share This Tip

Support Eclipse On E

3 thoughts on “Run/debug a class in Eclipse as a JUnit test or Java app with a single keystroke

  1. If you run a test, then right click a single testcase in the JUnit pane, you can run or debug it.
    After that, clicking the run icon in the JUnit pane will execute the same individual testcase, but Ctrl+F11 (and of course, alt-shift-x+t) runs the entire suite.

    It’d be nice if there was a keyboard shortcut for just running the individual testcase that was last run, especially when debugging a particularly annoying bug in one testcase, and double-especially if you’ve got the code under test full of debugging prints. It’s not the worst thing in the world but would be handy not to need the mouse 😀

    • Luckily there is a way to do this with the keyboard. Instead of running the individual test method from the JUnit view, go into the test class editor and position your cursor on the name of the test method you want to rerun. Then right-click and run as test (or even better Alt+Shift+X, T). Eclipse will not only run the individual test method, but it will also create a run configuration for the test (you can see it in Run > Run Configurations…).

      Now when you press Ctrl+F11, Eclipse will relaunch only the individual test method and not execute the whole test (also assuming you’ve followed the tip to make Ctrl+F11 always launch the last launched application). Hopefully this is what you were looking for.

    • Super! That’s exactly what I wanted.

      Can’t believe the first part was as easy as putting the cursor in the test method name first… simplicity FTW.

      Thanks 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *