Category Archives: Tips

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

Things to do after installing Ubuntu 13.04

Ubuntu 10.04

  • From Software Sources:
    1. select “Other Software”;
    2. check “Canonical Partners”;
  • install squid-deb-proxy-client to auto-discover caching-enabled machines in the local network by running::
    sudo apt-get install squid-deb-proxy-client -y
  • install some basic software by running in a terminal:
    sudo apt-get update && sudo apt-get install vim eclipse-platform \
    openjdk-7-jdk meld chromium-browser skype gimp inkscape \
    gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-plugins-bad \
    rar p7zip-full ttf-mscorefonts-installer flashplugin-installer keepass2 \
    python-launchpadlib python-apport gparted gpicview -y
  • Follow the Eclipse basic configuration and fixes;
  • remove unused applications:
    sudo apt-get --purge remove modemmanager thunderbird
  • install Dropbox (do not use nautilus-dropbox from Ubuntu Software Center because of this bug. If you have installed that accidentally, make sure you have uninstalled it and wiped out your ~/.dropbox directory before installing the version from their web site). If you are upgrading from previous versions of Ubuntu then uninstall first the client by running:
    rm -rf ~/.dropbox-dist
    sudo rm -rf /var/lib/dropbox/.dropbox-dist

    then to install the latest version run:

    dropbox start -i
  • to fix the broken/corrupted flash problem install the Flash-Aid add-on for Firefox and reboot for the change to take effect;
  • set up the fastest mirror by editing your /etc/apt/sources.list. The geographical location does not always give the best mirror: in order to choose the best mirror it is recommended to know your local internet network topology;
  • edit your .bashrc and change “alias ll=’ls -alF'” to “alias ll=’ls -lF'”;
  • Nautilus, Edit / Preferences / Default View = List View
  • to keep recent files from appearing in Unity: click on Privacy in the System Settings and disable Record Activity
  • to fix this bugaffecting LibreOffice run:
    sudo add-apt-repository ppa:om26er/unity
    sudo apt-get update && sudo apt-get upgrade

    and then reboot;

  • gedit Text Editor, Edit / Preferences:
    • check display right margin;
    • check highlight current line;
    • check highlight matching brackets;
    • uncheck create a backup copy of files before saving;
    • increase font size;
    • plugins, check Change Case and Sort;
  • Image Viewer, Edit / Preferences
    • Slideshow
      • uncheck “Expand images to fit screen”;
      • uncheck “Loop sequence”;
    • Plugins
      • check “Date in statusbar”;
  • set Libreoffice to use the metric system by default: open LibreOffice Writer and go to Tools / Options / LibreOffice Writer / General and set the measurement unit from the drop down menu called Measurement Unit. Do the same for each LibreOffice application;
  • set your search domains:
    1. with a DHCP server, edit your /etc/dhcp/dhclient.conf and add a line like the following for each of the domains you want to use:
      prepend domain-name "your.domain.com";
    2. with a static IP, edit your /etc/resolv.conf and add a line like the following for each of the domains you want to use:
      search your.domain.com
  • to rename and automount your NTFS partition:
    1. make required no folders in /mnt
      sudo mkdir /mnt/win
    2. check your partition list using
      sudo fdisk -l
    3. open /etc/fstab by running:
      sudo gedit /etc/fstab
    4. add entries in the following manner
      /dev/sdb1 /mnt/large ntfs user 0 0
    5. to make it appear in Nautilus open the directory by clicking on File System on the left menu, go into /mnt/win, from the top menu “Bookmarks” select “Add bookmark”.

Useful Linux howtos

Applications

  • to install Oracle Java JDK follow these instructions;
  • to compare files or directories use “Meld Diff Viewer”. You can install it by running:
    sudo apt-get install meld
  • to install Microsoft’s Core Font package run:
    sudo apt-get install ttf-mscorefonts-installer
  • to synchronize folders, files and make backups use “Conduit Synchronizer”. You can install it by running:
    sudo apt-get install conduit
  • to change grub settings (e.g. to change your default operating system, etc) install “StartUp Manager” by running:
    sudo apt-get install startupmanager
  • to automatically mount at boot your NTFS partitions install “Storage Device Manager” by running:
    sudo apt-get install pysdm
  • to set up a repository cache on your network so that once a package is downloaded from an official repository, all other machines will download it from your local area network, install squid-deb-proxy (and its client so the server downloaded updates get cached by the squid proxy and will also allow the server to install already-fetched updates via the proxy) on your server machine by running:
    sudo apt-get install squid-deb-proxy squid-deb-proxy-client

    then reboot your server machine. Clients can auto-discover caching-enabled machines in the local network by having the squid-deb-proxy-client package installed. To do that simply run:

    sudo apt-get install squid-deb-proxy-client

Information

  • to know your IP address:
    ifconfig | grep Bcast
  • to know your hardware details and save them in an html page run:
    lshw -html > your-file-name.html

    (a good idea is to generate one for each of your computers and save them up for future reference)

  • to check whether a certain package is installed:
    dpkg -l | grep packagename
  • to know whether you need to reboot a machine after updating the system via terminal check if the following file exists:
    /var/run/reboot-required
  • to test the speed of your hard drive run:
    sudo hdparm -t /dev/sd?
  • to list running services run:
    sudo netstat -tulpn
  • to check RAM speed, size and frequency:
    sudo dmidecode --type 17 | more
  • to know your processor details run:
    cat /proc/cpuinfo | grep name
  • to know the bios version of your motherboard run:
    sudo apt-get install smbios-utils
    sudo smbios-sys-info
  • to know the name of the Linux distribution installed on your machine run:
    lsb_release -a

    or, if that doesn’t work then run:

    cat /etc/issue
  • to have some system information of your machine run:
    uname -a
  • to see if your operating system architecture is 32-bit or 64-bit run:
    file /sbin/init
  • to list all users in your system from terminal, run:
    cat /etc/passwd | grep "/home" |cut -d: -f1

Operations

  • to configure your terminal commands history add one or more of the following lines to your ~/.bashrc file:
    • to change name of the file in which command history is saved:
      export HISTFILE=

      (the default value is ~/.bash_history)

    • to not save in the history certain command lines add them as a colon-separated list:
      export HISTIGNORE=
    • to not save a line matching any other previous entry in the history:
      export HISTCONTROL=ignoreboth
    • to change the number of commands to remember in the command history:
      export HISTSIZE=500

      (the default value is 500)

  • to add aliases system-wide you need to edit the file /etc/bash.bashrc For example, the following alias will provide a comprehensive update and clean operation:
    alias apt-update='sudo apt-get -f install
    && sudo apt-get autoremove -y
    && sudo apt-get autoclean -y
    && sudo apt-get clean -y
    && sudo apt-get update 
    && sudo apt-get upgrade -y --fix-missing'
  • to boot in text mode instead of graphical mode (GUI) open the /etc/default/grub file, locate the following line:
    GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash“

    and change it to:

    GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash text“

    and don’t forget to run ‘update-grub’ afterwards to update. If you need the graphical interface you can still have it by simply running “startx” once logged in.

  • to keep your computer’s time accurate follow these instructions;
  • to install fonts for system-wide run:
    mkdir /usr/share/fonts/truetype/myfonts
    cp [fonts] /usr/share/fonts/truetype/myfonts
    fc-cache -f -v
  • to add a new user via terminal run:
    sudo adduser username
  • to add a group to a userrun:
    sudo adduser username groupname
  • to modify the groups of a userrun:
    sudo usermod -G comma,separated,list,of,groupnames username
  • some useful command line shortcuts:
    • Ctrl-D: exit from the current session;
    • Ctrl-R: searching through the command history;
    • Ctrl-A: moves the cursor to the beginning of the command line;
    • Ctrl-E: moves the cursor to the end of the command line;
    • Ctrl-W: deletes the word immediately before the cursor;
    • Ctrl-K: deletes everything immediately after the cursor;
    • Ctrl-Y: undo a deletion.
  • to start a terminal maximized use the command
    gnome-terminal --window --maximize
  • to start a terminal in full screen use the command
    gnome-terminal --full-screen
  • to copy multiple files using scp use it like showed below:
    scp {file1,file2,file3} user@destination:~/
  • to speed up the ssh connection add “UseDNS no” (without quotation marks) at the end of your /etc/ssh/sshd_config and then run:
    sudo /etc/init.d/ssh reload
  • to add a new resolution to your monitor/display/screen run (assuming you want to add 1600×900):
    cvt 1600 900

    that will output something like this:

    Modeline "1600x900_60.00" 118.25 1600 1696 1856 2112 900 903 908 934 -hsync +vsync

    Copy the text following Modeline and paste it in the following command:

    xrandr --newmode "1600x900_60.00" 118.25 1600 1696 1856 2112 900 903 908 934 -hsync +vsync

    Then run the command xrandr alone:

    xrandr

    and note down the connected primary video source (should something like VGA-0, or VGA1). Supposing it’s VGA-0 then run:

    xrandr --addmode VGA-0 "1600x900_60.00"
  • to open/extract an eml file install munpack:
    sudo apt-get install mpack

    then run:

    munpack filename.eml -t
  • to chmod 775 sub-directories, files excluded:
    find /path/to/base/dir -type d -exec chmod 775 {} +

    to chmod 664 files recursively, sub-directories excluded:

    find /path/to/base/dir -type f -exec chmod 664 {} +