Saturday, 9 June 2012

Centos 6/RHEL Java JDK7 with Eclipse & Ant installation

This tutorial is to install and set up Sun Java JDK 7 and Eclipse on the Centos 6 as described here or Fedora/Red Hat and rpm based distros. I prefer to use Sun Java 7 as the default JDK so I dont see much point in running the openJDK version, I generally remove it if it gets installed. Some programs such as Jenkins and IntelliJIDEA require the Sun version, so you might as well use it exclusively, it is free to use after all.

Additionally, the JDK includes the JRE so again you may as well just use the JDK and configure browser plugins to use its JRE with the 'alternatives' command as you will see below. If you just want Java JDK then you can ignore the section on Eclipse.

If you are using the Android SDK with Eclipse you might want to stick with the 32 bit OS, as 64 bit is a little tricky to get working properly, but its entirely up to you.


Setting up JDK 7


Install jpackage-utils if not already installed


$ sudo yum -y install jpackage-utils

You will need Java before installing Eclipse so first download it from http://www.oracle.com  The current version is jdk-7u6.


For 32bit use jdk<file_name>i586.rpm package


$ sudo rpm -Uvh <file_name>.rpm   

  
For 64bit use jdk<file_name>x64.rpm version

$ sudo rpm -Uvh <file_name>.rpm

(Ignore missing file errors)  

 Add the JAVA_HOME path/variable to ~/.bashrc

$ sudo vi ~/.bashrc      


Place as below under 'User specific aliases and functions'

export JAVA_HOME="/usr/java/jdk1.7.0_06"

export JAVA_PATH="$JAVA_HOME"
export PATH="$PATH:$JAVA_HOME"

See Vi Editor for commands


Change the version number to yours, save & source the file

$ source ~/.bashrc
     
Make sure the alternatives is set correctly, if its not

$ sudo rm /var/lib/alternatives/java     


Will purge the old setup.

(On a new installation it may show 'not found')

then set it up again with your new jdk.     


$ sudo alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_06/bin/java 1

$ sudo alternatives --config java     

Hit enter for the default or select a number

This will set your new jdk as the default alternative, to check


$ alternatives --display java   


java - status is manual.

 link currently points to /usr/java/jdk1.7.0_06/bin/java
/usr/java/default/bin/java - priority 1
Current `best' version is /usr/java/jdk1.7.0_06/bin/java.

$ which java                           

/usr/bin/java         


$ java -version                        


java version "1.7.0_06"

Java(TM) SE Runtime Environment (build 1.7.0_06-b24)
Java HotSpot(TM) Server VM (build 23.2-b09, mixed mode)   

$ echo $JAVA_HOME                     


/usr/java/jdk1.7.0_06


Reboot.


If you just wanted Java you can skip the next chapter and go to the one below on browser plugins.

Setting up Eclipse


Now go to http://www.eclipse.org and download the Eclipse IDE version you require to your home directory. Get the 'tar.gz' variant. I use the Indigo 3.7 version as I find it works fine.


Eclipse is in the Centos repos, however, that version also brings with it a lot of other packages which you may not need like openJDK and a few others, so I find it better to just get an eclipse.org version and add to it what I require.


$ cd /usr/local         


$ sudo su        


# tar xvf ~/<eclipse_tar_file_name>


   So you have eclipse installed at /usr/local/eclipse/

   Now change the permissions to run it

# chown -R <user_name> eclipse

# chmod -R 700 eclipse

Revert back to normal user


# exit


create a symlink to /usr/bin

$ sudo ln -s /usr/local/eclipse/eclipse /usr/bin/eclipse


Create a symlink/shortcut to open it

$ cd <path_to_where _you _want _the _shortcut>


$ ln -s /usr/bin/eclipse              


Now you can do Alt+F2 and type 'eclipse' or click on your shortcut wherever you placed it to run eclipse. (I put one in the Desktop folder of thunar) If you are using Fluxbox you can put a command in the 'keys' file in the /home/<user_name>/.fluxbox hidden directory - enter at the top something like 'F4 : ExecCommand eclipse' and you can run it by hitting F4 or whatever.


Apache Ant 

You can install Ant now if you are going to use it, the binary 1.7 version can be obtained from below, it's for any arch.

Ant-1.7.1noarch.rpm 

You may need to install dependencies

$ sudo yum -y install java-devel xerces-j2 xml-commons-apis

get it in to your home directory then install it with

$ sudo rpm -Uvh ant-1.7.1-7.jpp5.noarch.rpm

Set up a few extra variables now in your ~/.bashrc

$ sudo vi ~/.bashrc 

If you have set up the $JAVA_HOME as above, then between the 
JAVA_PATH & PATH variables, first place your ANT_HOME

ANT_HOME="usr/share/ant"          

and append it to the PATH so your file looks similar to below

export JAVA_HOME="/usr/java/jdk1.7.0_06"
export JAVA_PATH="$JAVA_HOME"
ANT_HOME="usr/share/ant"
export PATH="$PATH:$JAVA_HOME:$ANT_HOME"


You may want to put in the CLASSPATH export and the JAVA_FONTS if you are using them.

export CLASSPATH=.:/usr/java/jdk1.7.0_06/lib/tools.jar
JAVA_FONTS=/usr/share/fonts/truetype
export JAVA_FONTS

Don't forget to source the file when done.

$ source ~/.bashrc

So your .bashrc file might now look similar to below

export JAVA_HOME="/usr/java/jdk1.7.0_06"
export JAVA_PATH="$JAVA_HOME"
export CLASSPATH=.:/usr/java/jdk1.7.0_06/lib/tools.jar
ANT_HOME="usr/share/ant"
export PATH="$PATH:$JAVA_HOME:$ANT_HOME"

JAVA_FONTS=/usr/share/fonts/truetype
export JAVA_FONTS

Leave off anything you are not using.

With this all done you should now be able to run Ant directly from the command line after moving to the directory containing your build.xml file in the workspace using the command

$ ant -f build.xml

So Ant can be used both in and outside of Eclipse.

See Fluxbox key bindings for more.


Fixing Browser plugins 


Firefox is in the standard repos and Google-Chrome is available as an rpm package so installing is straighforward.


First, make sure Firefox is installed


(Needed for the plugins directory)


$ sudo yum -y install firefox


You may have to create a plugins directory, if so


$ sudo mkdir /usr/lib/mozilla/plugins      


To fix the java plugins issue the following commands

32bit

$ sudo alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/latest/jre/lib/i386/libnpjp2.so 20000


64bit

$ sudo alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/jdk1.7.0_06/jre/lib/amd64/libnpjp2.so 20000

this will install the plugin using the JRE from within the JDK package, no need for a separate JRE package.

Oracle's site and other web tuts state you should use the 32 bit JRE for web applets on a 64bit system, I have found this unnecessary and just use the JDK above.

Now visit http://javatester.org/ with firefox to test the java plugin works. You should find it does and also that java in chrome works now aswell.


So thats it, installed Java 7 and plugins, with Eclipse from eclipse.org, using just 2 downloaded packs and without messing up your system with a lot of other things you dont require like openJDK.


You can switch between installed versions like 6 & 7 and others.

Other 'alternatives' commands for java


  • java
$ sudo alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_06/jre/bin/java 20000


  • javaws


$ sudo alternatives --install /usr/bin/javaws javaws /usr/java/jdk1.7.0_06/jre/bin/javaws 20000


  • Java Browser (Mozilla) Plugin 32-bit 

$ sudo alternatives --install /usr/lib/mozilla/plugins/libjavaplugin.so libjavaplugin.so /usr/java/jdk1.7.0_06/jre/lib/i386/libnpjp2.so 20000


  • Java Browser (Mozilla) Plugin 64-bit 

$ sudo alternatives --install /usr/lib64/mozilla/plugins/libjavaplugin.so libjavaplugin.so.x86_64 /usr/java/jdk1.7.0_06/jre/lib/amd64/libnpjp2.so 20000


  •  javac if you have JDK (Java Development Kit) 

$ sudo alternatives --install /usr/bin/javac javac /usr/java/jdk1.7.0_06/bin/javac 20000


  • jar if you have JDK (Java Development Kit)

$ sudo alternatives --install /usr/bin/jar jar /usr/java/jdk1.7.0_06/bin/jar 20000


I have put together an Eclipse Keplar with Java JDK which runs from a USB device on Linux systems, (any, not just Centos) I have tested it on a couple of systems and it appears to be fine.

You can download and try it from the link below. When you have got it in your home folder move in to it and chmod the Eclipse executable before running it, it will find its own Java in the enclosed jre directory and it will probably ask to create a workspace in /home/<user>.

$ cd eclipse

$ chmod u+x eclipse

Download Portable Eclipse with JDK.