Setting up Ubuntu 12.04 for Java development

15:21 , , , 13 Comments

In this post I will describe steps I have followed after fresh install of new Ubuntu 12.04 LTS. Linux is great environment for Java development and I am using for several years now. In my last work I had to maintain projects running on different versions of Java. This instruction gives some hints how you can deal with this situation effectively. Moreover I describe how to change GUI outline in Eclipse to be more compact using GTK configuration files.

JDK

I prefer installing Java manually. As I mentioned before I need to have several versions of Java including 1.5, 1.6 and 1.7. First step is to download required files from oracle site:
http://www.oracle.com/technetwork/java/javase/downloads

Choose Linux files for your OS architecture (i586 for 32-bit version of Ubuntu and x64 for 64-bit version). You should download files with .bin extension (for 1.5 and 1.6 JDK versions) or .tar.gz (1.7 JDK). After downloading, you should have following files in Downloads directory:
stanislaw@latitude:~/Downloads$ ls -l
total 212888
-rw-rw-r-- 1 stanislaw stanislaw 49760685 Jun  8 14:42 jdk-1_5_0_22-linux-i586.bin
-rw-rw-r-- 1 stanislaw stanislaw 85292206 Jun  8 14:43 jdk-6u31-linux-i586.bin
-rw-rw-r-- 1 stanislaw stanislaw 82927766 Jun  8 14:37 jdk-7u4-linux-i586.tar.gz
We will install all jdk's in /opt/java directory. First create directory:
sudo mkdir /opt/java
Now extract jdk-7 file and simply move it do /opt/java directory:
stanislaw@latitude:~/Downloads$ tar zxvf jdk-7u4-linux-i586.tar.gz
stanislaw@latitude:~/Downloads$ sudo mv jdk1.7.0_04/ /opt/java
To install jdk 1.5 and 1.6 first make bin files executable with chmod command:
stanislaw@latitude:~/Downloads$ chmod +x jdk-1_5_0_22-linux-i586.bin jdk-6u31-linux-i586.bin
Now run this executables (you will be shown license agreement; press q and confirm it by typing yes):
stanislaw@latitude:~/Downloads$ ./jdk-1_5_0_22-linux-i586.bin
...
stanislaw@latitude:~/Downloads$ ./jdk-6u31-linux-i586.bin
...
Right now 1.5 and 1.6 JDK are in Downloads directory. Move them to /opt/java:
stanislaw@latitude:~/Downloads$ sudo mv jdk1.5.0_22/ /opt/java/
stanislaw@latitude:~/Downloads$ sudo mv jdk1.6.0_31/ /opt/java/
Create symbolic links in /opt/java directory. This will simplify updating process to changing particular symbolic link.
stanislaw@latitude:~/Downloads$ cd /opt/java/
stanislaw@latitude:/opt/java$ sudo ln -s jdk1.5.0_22 jdk-1.5
stanislaw@latitude:/opt/java$ sudo ln -s jdk1.6.0_31/ jdk-1.6
stanislaw@latitude:/opt/java$ sudo ln -s jdk1.7.0_04/ jdk-1.7
Now it is time to set up java, javac and java plugin. It is best done with update-alternatives utility program. More information about this setup can be found https://sites.google.com/site/easylinuxtipsproject/java. Please note usage of paths to symbolic links created in previous step.
stanislaw@latitude:/opt/java$ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk-1.5/bin/java" 2
stanislaw@latitude:/opt/java$ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk-1.6/bin/java" 1
stanislaw@latitude:/opt/java$ sudo update-alternatives --install "/usr/bin/java" "java" "/opt/java/jdk-1.7/bin/java" 3

stanislaw@latitude:/opt/java$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk-1.7/bin/javac" 3
update-alternatives: using /opt/java/jdk-1.7/bin/javac to provide /usr/bin/javac (javac) in auto mode.
stanislaw@latitude:/opt/java$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk-1.6/bin/javac" 2
stanislaw@latitude:/opt/java$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/opt/java/jdk-1.5/bin/javac" 1

sudo update-alternatives --install "/usr/lib/mozilla/plugins/libjavaplugin.so" "mozilla-javaplugin.so" "/opt/java/jdk-1.6/jre/lib//i386/libnpjp2.so" 1
Last step is setting JAVA_HOME environment variable in .bashrc. Simply add this line at the end of file, to keep java 1.6 as default. Please note that we do not need to set PATH variable, because we used update-alternatives utility to setup /usr/bin/java symbolic link. When I need to work with different version of Java, I simply override default JAVA_HOME value for specific application, i.e. JBoss AS.
export JAVA_HOME=/opt/java/jdk-1.6

Eclipse

Download Linux version, extract it, and move to /opt directory:
stanislaw@latitude:~/Downloads$ tar zxvf eclipse-jee-indigo-SR2-linux-gtk.tar.gz
stanislaw@latitude:~/Downloads$ sudo mv eclipse /opt/eclipse
Application should be ready to run with default executable /opt/eclipse/eclipse. In gtk environment spacing between GUI components seam to be to big. This can be hopefully fixed by customized gtk configuration based on this blog post. One thing I have added, is tool tip background and foreground configuration, to make it visible (see bug report).
stanislaw@latitude:/opt/eclipse$ cat eclipse-gtkrc
style "gtkcompact" {
font_name="Sans 9"
GtkButton::default_border={0,0,0,0}
GtkButton::default_outside_border={0,0,0,0}
GtkButtonBox::child_min_width=0
GtkButtonBox::child_min_heigth=0
GtkButtonBox::child_internal_pad_x=0
GtkButtonBox::child_internal_pad_y=0
GtkMenu::vertical-padding=1
GtkMenuBar::internal_padding=0
GtkMenuItem::horizontal_padding=4
GtkToolbar::internal-padding=0
GtkToolbar::space-size=0
GtkOptionMenu::indicator_size=0
GtkOptionMenu::indicator_spacing=0
GtkPaned::handle_size=4
GtkRange::trough_border=0
GtkRange::stepper_spacing=0
GtkScale::value_spacing=0
GtkScrolledWindow::scrollbar_spacing=0
GtkExpander::expander_size=10
GtkExpander::expander_spacing=0
GtkTreeView::vertical-separator=0
GtkTreeView::horizontal-separator=0
GtkTreeView::expander-size=8
GtkTreeView::fixed-height-mode=TRUE
GtkWidget::focus_padding=0
}
class "GtkWidget" style "gtkcompact"

style "gtkcompactextra" {
xthickness=0
ythickness=0
}
class "GtkButton" style "gtkcompactextra"
class "GtkToolbar" style "gtkcompactextra"
class "GtkPaned" style "gtkcompactextra"

style "tooltip" { bg[NORMAL] = "#FFFFFF" fg[NORMAL] = "#000000" }
widget "gtk-tooltips" style "tooltip"
In order to override gtk configuration for eclipse only, it is most convenient to prepare additional script eclipse.sh with GTK2_RC_FILES environment variable set.
#!/bin/sh

export GTK2_RC_FILES=/opt/eclipse/eclipse-gtkrc

/opt/eclipse/eclipse
Last step is to add launch icon. This can be done in several ways described here. I prefer method with manual creation of .desktop file. It can be easily used to add launch icon for any other application installed this way.
stanislaw@latitude:~$ cat /usr/share/applications/eclipse.desktop 
[Desktop Entry]
Name=Eclipse
Exec=/opt/eclipse/eclipse.sh
Icon=/opt/eclipse/icon.xpm
Type=Application
StartupNotify=true
Categories=Development

Other

Installing other java development software like maven, ant is simply done by extracting downloaded zip archives (I personally always use /opt directory) and setting specific environment variables like PATH and ANT_HOME. During my fresh install I installed additionally following packages:
# package libsvn-java is required by subclipse plugin
sudo apt-get install libsvn-java
# version control
apt-get install git svn
# my favorite terminal editor
sudo apt-get install vim
# MS fonts
sudo apt-get install ttf-mscorefonts-installer
# testing frontend configuration for production environment with apache
apt-get install apache2
# ... and many more

Stanisław Ogórkis

Software engineer specializing in delivering highly interactive web applications and complex enterprise systems.

13 comments: