How to install a Java Development Workstation on Ubuntu ?

Java SDK

Install the Java Development Kit :

$ sudo apt-get install openjdk-6-jdk
$ sudo apt-get install openjdk-7-jdk

Java version is according to your own needs.
You can also install the Oracle JDK edition :

$ sudo add-apt-repository -y ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install -y oracle-jdk7-installer
$ update-alternatives --display java

Eclipse

Go to http://www.eclipse/org/downloads/

then download the JavaEE version.

Prepare

first prepare a directory where to install all java software :

$ sudo mkdir -p /opt/tools/java/eclipse

then prepare a specific directory for your multiple eclipse version (if needed) :

$ sudo mkdir -p /opt/tools/java/eclipse/3.7 # for Indigo 
$ sudo mkdir -p /opt/tools/java/eclipse/4.2 # for Juno

Unzip

now, unzip downloaded file :

$ tar -zxvf eclipse-indigo-*.tar.gz 
$ sudo mv eclipse/* /opt/tools/java/eclipse/3.7/ 
$ rm eclipse 
$ tar -zxvf eclipse-juno-*.tar.gz 
$ sudo mv eclipse/* /opt/tools/java/eclipse/4.2/ 
$ rm eclipse

and then set rights to those new files and change owner to root :

$ chmod -R 755 /opt 
$ chown -R root:root /opt

Ok, now how to create a shortcut to execute such software ?

Script !

First, create a symbolique link to the needed eclipse version :

$ ln -s /opt/tools/java/eclipse/3.7 /opt/tools/java/eclipse/eclipse

ok, and now create a shell script to start eclipse:

$ sudo touch /usr/bin/eclipse 
$ sudo nano /usr/bin/eclipse

paste the following lines :

#!/bin/sh 
export ECLIPSE_HOME=$JAVA_TOOLS_HOME/eclipse/$ECLIPSE_VERSION 
$ECLIPSE_HOME/eclipse $1

And set rights and owner :

$ sudo chmod 755 /usr/bin/eclipse 
$ sudo chmod root:root /usr/bin/eclipse

ok.

Desktop

create a new file :

$ sudo nano /usr/share/applications/eclipse.desktop

and the copy the next lines into :

[Desktop Entry] 
Name=Eclipse  
Type=Application 
Exec=/opt/tools/java/eclipse/eclipse/eclipse 
Terminal=false 
Icon=/opt/tools/java/eclipse/eclipse/icon.xpm 
Comment=Integrated Development Environment 
NoDisplay=false 
Categories=Development;IDE 
Name[en]=eclipse.desktop

Maven ?

Yes, to work with Java, we need a build tool. here we will use Maven 3.0.4.

Download and install

browser the http://maven.apache.org/download.html and download the *.tar.gz file edition for version 3.0.4.

create a target directory:

$ sudo mkdir -p /opt/tools/java/maven/3.0.4

unzip and move the files to /opt target :

$ tar -zxvf apache-maven-3.0.4.tar.gz 
$ mv apache-maven-3.0.4/* /opt/tools/java/maven/3.0.4

ok, now maven is installed.

Environment

We have to set the MVN_HOME and the MVN_REPO.

$ sudo nano /etc/skel/.bashrc

copy at end of file:

. /etc/default/javaenvsettings

Note:
Beware of the space character between “.” and “/”.

and create a /etc/default/javaenvsettins file

$ sudo gedit /etc/default/javaenvsettings

add the following lines:

#!/bin/sh 
# Tools Environment settings 
#-------------------------- 
export ECLIPSE_VERSION=3.7 
export MAVEN_VERSION=3.0.4 
# If exists execute user settings 
if [ -f ~/.javaenvsettings ]; then
   . ~/.javaenvsettings fi # set Tools path 
export TOOLS_HOME=/opt/tools 
export JAVA_TOOLS_HOME=$TOOLS_HOME/java 
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-i386/ 
# set Maven Settings 
export MAVEN_HOME=$JAVA_TOOLS_HOME/maven/$MAVEN_VERSION
export M2_HOME=$MAVEN_HOME 
export M2=$MAVEN_HOME/bin 
# set Eclipse Path 
export ECLIPSE_HOME=$JAVA_TOOLS_HOME/eclipse/$ECLIPSE_VERSION
# Path modification according to Tools Settings 
export PATH=$PATH:$M2:$ECLIPSE_HOME

and to perform the last “touch”, just set rights and owner for the file:

$ sudo chmod 755 /etc/default/javaenvsettings 
$ sudo chown root:root /etc/default/javaenvsettings

You have certainly noticed the execution of a ~/.javaenvsettings file.

If such file exists, user can set its own values for java environement tools versions:

#!/bin/sh 
# Uncomment the line to activate the needed eclipse version. 
#export ECLIPSE_VERSION=4.2 
#export ECLIPSE_VERSION=3.7

Don’t forget rights and owner :

$ sudo chmod 755 /etc/skel/.javaenvsettings 
$ sudo chown root:root /etc/skel/.javaenvsettings

You can create such file and copy it to /etc/skel/.javaenvsettings so a default file, all commented, will be automatically created with a new user home.

Done !

Note:
if you are already connected, your existing users don’t have such execution in their ~/.bashrc file. So don’t forget to edit files to add “. /etc/defaults/javaenvsettings” at end of “.bashrc” file.

That’s all Folks !

Conclusion

Now each new user created on the workstation would have a java environment already set.
just start a temrinal and execute eclipse or mvn command to run needed tools.

Have Fun !

McG.

Publicités