Always run/debug the last launched class instead of the selected one in Eclipse

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.

Continue reading

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.

Continue reading

log4j logging made easier with Eclipse templates

Logging statements have taken up a nice chunk of my programming life. Info, debug and warning statements are common, especially since some team standards require that logging be done consistently (eg. at the start and end of all methods). Take the following code:

import org.apache.log4j.Logger;
public class Engine {
    private final static Logger logger = Logger.getLogger(Engine.class);

    public void someMethod() {
        logger.debug("Doing something here");

To get the statement on line 8 you first have to insert line 4, generate the import on line 1 (eg. with Ctrl+Shift+O) and then move back to line 8 to type out the statement. That’s a lot of time for just one statement.

So here are some templates to help with this. The templates assume log4j since most people use that (right?), but can easily be adapted for Java Logging or other framework. If you’re new to templates, then see this tip to get an overview.

Continue reading

Switch and close editors faster with the keyboard using Eclipse’s Quick Switch Editor

I spend a huge amount of my programming time in editors, editing, switching between them and closing them. This means that managing editors has to be as efficient as possible, which means that, for one thing, I shouldn’t be swapping between the mouse and keyboard all the time to work with editors.

Luckily, there is a way to manage editors using the keyboard (and it doesn’t involve the laborious Ctrl+F6). It’s a feature called Quick Switch Editor and it makes working with editors a lot more enjoyable.

Quick Switch Editor works by popping up a list of all open editors (sorted by editor title) to the right of the editors tabs. The list is searchable so you don’t have to navigate long lists with the arrow key and searching supports wildcards. On top of that, you can also close editors from the list. Here’s a quick glimpse of what it looks like:

Continue reading

Instantly show a class/file in the Package/Project Explorer in Eclipse

While working on a class or XML file, you’ll often find that you want to work with the actual .class or .xml file in the Package Explorer, Project Explorer or Navigator views. You may want to copy, move or delete the file or do stuff that are only accessible in these views (eg. browse all files in the same folder).

Eclipse has a feature called Show In, that’s almost like a Quick Open. It instantly navigates to the current editor’s file in a requested view and positions you directly on the file in that view, no matter how deep in the hierarchy the file is. It’s also keyboard friendly, which means no more reaching for the mouse.

This feature is perfect for those (like me) who don’t like the Link with Editor functionality on certain views. I don’t like the Package Explorer to Link with Editor as I like to know things are the way I left them (see the rant at the end of the post).

Continue reading

Split and view the same editor side by side in Eclipse

At some point you’ll want to view the same class or file side by side. You may want to follow related code in different parts of the class or need some code constantly available as reference to change some other part of the class.

Eclipse allows you to split an editor and move it to anywhere in the editor area in the same window, including next to the original editor. Changes made in the one editor are reflected in the other. The feature isn’t very obviously named, but it is easy to use.

Continue reading

Setup Eclipse breakpoints to stop only under certain conditions

Sometimes while debugging, you may have wanted to stop at a breakpoint only when a certain condition applies. To do this you may have used following coding pattern:

public void doSomething(Message message) {
    if (message == null) {
    	System.out.println("message is null");

    // Do something with message that doesn't handle nulls well, eg...
    if (message.isFormatted() {

It turns out lines 2-4 are temporary lines that will be removed later. They are only required for debugging so you can set a breakpoint on line 3. Although effective, this approach has its drawbacks. Firstly, you’re adding temporary code to production code and have to remember to remove it before checking in the eventual fix. Another problem arises if you want to stop only after a method has been called n times. You can introduce counting code but you’re stuck with the same problem as before.

There’s an Eclipse feature called conditional breakpoints that makes this a lot easier. The condition is attached to the breakpoint so doesn’t interfere with existing code and you can stop conditionally after a number of calls.

Continue reading

Select entire strings and methods in Eclipse with a single keystroke

Selecting strings is a common thing especially if you want to (a) extract a constant representing it, (b) replace the string with a message bundle key or (c) use it as a search term to look for the same string across the codebase. When refactoring, you’ll sometimes want to delete a method or move it around, requiring you to select the entire method.

The problem is that selecting these can be slow. To select a string you’ll typically move the cursor to the start of the string then select the string using the arrow keys or Ctrl+Shift+Right (select next word) repeatedly until you reach the end of the string which can be slow depending on the number of words in the string. The strategy for methods is similar, move to the start and select but using .

Eclipse has a keyboard shortcut called Select Enclosing Element to make this faster. It allows you to select entire strings from anywhere within the string and methods from anywhere within the method. A bonus is that you do the same thing no matter how big the string/method is.

Continue reading

How to share plugins across multiple Eclipse installations and reinstall Eclipse easily

Eclipse’s big strength is its plugins – they make it easy to add new features to the IDE. But plugins can make life difficult when you have to reinstall Eclipse (eg.  moving up a version) or have multiple installations (eg. for C++, Java and PHP). Because plugins are normally installed in the plugins folder underneath your Eclipse installation, they aren’t shared by other installations

You could always install the plugins again and again for each installation. That would mean for Subclipse (or favourite), your favourite XML editor, UML tool, etc. This is extremely slow and frustrating.

There is a feature that makes life easier though. You can copy plugins to an external folder and create shortcuts to them via link files. Then you can copy these link files to any Eclipse installation.

Continue reading

Automatically format and cleanup code every time you save

Eclipse has a wonderful feature that formats according to your coding standards. It handles things like indentation, where curly braces are placed, if blank lines should occur between field declaration and hundreds of other options.

However, to invoke this formatting, you have to tell Eclipse to do this every time you’ve edited the code. You can do this using Ctrl+Shift+F but (1) you have to do that every time and (2) you have to remember to do it in the first place (and we all know how good developers are at remembering things).

Eclipse sports a feature called Save Actions that makes formatting code easy. With this feature enabled, every time you save the file, Eclipse formats the code to your formatting preferences and does some cleanup of the code (eg. remove unused imports). All done automatically and for free. Continue reading