I previously mentioned shortcuts to run/debug a class as a Java app/JUnit test. But have you ever tried to rerun the last application you launched in Eclipse using F11 or the toolbar, only to find out it’s running the currently active class or selection in Package Explorer? Well, that’s because Eclipse’s default is to run the selected resource or active editor if it’s launchable (eg. if it has a main method or is a JUnit test).
Luckily, there is a preference you can change to force Eclipse to always run the last application you launched. So now you can avoid the confusion and rest assured that it’ll do what you want it to do.
How to run the last launched application
The answer lies in a preference hidden on the Run/Debug page:
- Go to Window > Preferences > Run/Debug > Launching.
- Select the option Always launch the previously launched application. It’s located at the bottom of the dialog.
The preference should look something like this:
Now you’re free to press F11, Ctrl+F11 or click the Run/Debug icons on the toolbar with confidence, knowing it’s going to run what you expect it to run.
One question arises: How do you quickly run a class as a specific application (eg. a JUnit test) if you can’t press F11 to run it? You have some options available:
- The fastest way is to use keyboard shortcuts. Eclipse allows you to launch classes using a keystroke, including JUnit tests, Java applications, etc using Alt+Shift+X.
- Alternatively, right-click on the class and select Run As. The submenu will show available options for launching the class.
- Lastly, you could click the pulldown arrow on the run/debug icons in the toolbar and select Run As. The submenu once again shows available options for launching the class.
So with this out of the way, here are some other tips that may interest you:
- As mentioned above, you can launch a class using a keystroke without having to reach for the mouse.
- There are also some other preferences you can play around with that may help remove annoyances or speed up your work, because Eclipse doesn’t always get it right the first time.