Tag Archives: eclipse

Eclipse suggested settings, shortcuts, templates and troubleshooting

eclipseI have been using the Eclipse IDE for coding for several years, mostly in Java but also in PHP, HTML, CSS and Javascript. With this post I would like to share my favorite settings, Java templates and shortcuts I use on a daily basis, and some troubleshooting I had to deal with.

Suggested settings

Window / Preferences / General

  • Editors / Text editors
    • Undo history size = 99999
    • Show print margin
    • Show line numbers
    • Spelling / Disable spell checking
  • Web Browser = Use external web browser
  • Workspace
    • Refresh using native hooks or polling
    • Text file encoding: UTF-8

Window / Show View / Tasks

Package Explorer

  • View menu / Package presentation = hierarchical
  • “Link with editor” selected

Java templates

  • try finally block

Name = tryf
Description = try finally block
Pattern:

try {
    ${line_selection}${cursor}
} finally {
    // ${todo}: cleanup code
}
  • explicit lock-unlock block

Name = lul
Description = explicit lock-unlock block
Pattern:

${lock_name}.lock();
try {
    ${line_selection}    
} finally {
    ${lock_name}.unlock();
}
  • iterate over map

Name = formap
Description = iterate over map
Pattern:

${:import(java.util.Map)}
for (Entry<${key_type}, ${value_type}> entry : ${map}.entrySet()) {
    ${key_type} key = entry.getKey();
    ${value_type} value = entry.getValue();
    ${cursor}
}
  • read a file through scanner

Name = fread
Description = read a file through scanner
Pattern:

${:import(java.io.BufferedInputStream,
          java.io.FileInputStream,
          java.io.FileNotFoundException,
          java.util.Scanner)}
try (Scanner input = new Scanner(new BufferedInputStream(
        new FileInputStream(${file_name})))) {
    ${cursor}
} catch (FileNotFoundException e) {
    // ${todo}: handle exception
}
  • write a file through printwriter

Name = fwrite
Description = write a file through printwriter
Pattern:

${:import(java.io.FileWriter,
          java.io.IOException,
          java.io.PrintWriter)}
try (PrintWriter output = new PrintWriter(new FileWriter(${file_name}))) {
    ${cursor}
} catch (IOException e) {
    // ${todo}: handle exception
}

Favorite shortcuts

CTRL+SHIFT+F Format source code = automatic indentation
ALT+SHIFT+R Refactor = Renames the selected element and (if enabled) corrects all references to the elements (also in other files)
CTRL+SHIFT+O Organize the imports = adds the missing import statements and removes the unused ones
CTRL+O Shows all methods of the current class, press CTRL+O again to show the inherited methods
CTRL+SHIFT+T Types search = quick access to a class of the current project
CTRL+left mouse click Opens the declaration or implementation of a variable, constant, method or class
ALT+right/left arrow Goes to previous or next editor position in history
CTRL+D Deletes the current line
CTRL+M Maximizes/minimizes the selected panel
ALT+SHIFT+N Opens a menu for the quick creation of a new element (Class, Interface, Package, etc)
CTRL+1 Quickfix = shows potential fixes for warnings, errors or shows possible actions
CTRL+F11 Runs the current class as Java application or the last executed application, depending on the preferences (by default it’s the last executed application)
CTRL+PGUP/PGDOWN Switch forward/backwards the current selected editor from the currently opened editors
CTRL+E Allows to select an editor from the currently opened editors
SHIFT+F2 Shows the Javadoc for the selected type / class / method

Keep in mind that you can quickly access any Eclipse element by typing in the Quick Access in the top right corner of the window.

A more comprehensive list can be found at ZeroTurnaround:

Troubleshooting

  • to fix the problem with Unity scroll bar, which doesn’t work in Eclipse, run:
    sudo vim /usr/bin/eclipse

    and insert the following lines at the beginning of the file:

    # work around for LP #769277
    export UBUNTU_MENUPROXY=0
    export LIBOVERLAY_SCROLLBAR=0
    # end work around
  • to fix the problem of Eclipse crashing at startup displaying an error dialog with the message “An error has occurred. See the log file /home/user/.eclipse/org.eclipse.platform_3.7.0_155965261/ configuration/1334402764482.log.” run the following commands:
    sudo apt-get install libswt-gtk-3-jni libswt-gtk-3-java
    sudo cp /usr/lib/jni/libswt-*.so ~/.swt/lib/linux/x86_64/
    sudo chown myusername. ~/.swt/lib/linux/x86_64/*
    sudo chmod 755 ~/.swt/lib/linux/x86_64/*

    Replace x86_64 with x86 if you have a 32-bit machine;

  • Eclipse on Linux uses by default  UTF-8 as text encoding, while on Windows it uses the CP1252 one: when you transfer your source code from one system to the other some special characters (like the accented vowels) may not appear correctly in one of the systems. To fix this problem open your Eclipse on Windows and change its text encoding from CP1252 to UTF-8 from the menu Window → Preferences → General → Workspace, in the “Text file encoding” panel. To change the encoding of a file from CP1252 to UTF-8 run the following command on a Linux terminal:
    iconv --from-code=WINDOWS-1252 --to-code=UTF8 File.java