Sunday, April 22, 2007

How to install Java SDK (1.6) and Apache Ant (1.7) on CentOS 4.4 (Server CD)

I had enough with some package managers or alternative ways of installing Java on a server which is running CentOS (RHEL) 4.4 . I think the most straight forward way is the following. I promise by the end of this post you will have Java and Apache Ant running on you server!

SETUP JAVA

Step 1: Download Java SDK for Linux (I have used the .bin file)

Step 2: upload the .bin file to your server - I have picked the following folder /usr/lib/
So it would be /usr/lib/jdk-6u1-linux-i586.bin (this is JDK1.6 update 1 for my case maybe when you read this post is jdk1.7).

Step 3: Unpack - extract the bin file contents of the bin file
> chmod 777 jdk-6u1-linux-i586.bin
> ./jdk-6u1-linux-i586.bin

Step 4: Follow the instructions and type yes when prompted to do so! After unpacking you should have the following folder /usr/lib/jdk1.6.0_01/

Step 5: Go to the /etc/alternatives folder and create a sym link to the java executable

> cd /etc/alternatives
> ln -s /usr/lib/jdk1.6.0_01/bin/java java
Step 6: Go to the /usr/bin folder and create a sym link pointing to the sym link of alternatives
(there are 2 levels of sym links that may prove to be handy in case we change to a new version after a while)

> cd /usr/bin/
> ln -s /etc/alternatives/java /usr/bin/java

Yes this is it you may test if Java( runtime) is there by typing

> java -version

Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)

SETUP JAVA_HOME

Step 1: Based on the installation above I would like to create another sym link to point on the jdk folder so

> cd /etc/alternatives
>ln -s /usr/lib/jdk1.6.0_01/ /etc/alternatives/jdk
So if I do a
> cd /etc/alternatives/jdk
>ls

I get the contents of the actuall jdk160 folder

Step 2: Set the JAVA_HOME variable globally!

> cd /etc/
> vi ./profile

(hope you know the basics of vi)

Step 2.1 :there add a new line with the following
JAVA_HOME ="/etc/alternatives/jdk"

Step 2.2: and change the existing line
from
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
to
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC JAVA_HOME


Step 3: you should restart your bash session in order the new variable to be loaded just check
> echo $JAVA_HOME
/etc/alternatives/jdk
Done!



SETUP APACHE ANT

Step 1:
Go to the /usr/lib/ folder and wget apache ant gz file
>cd /usr/lib/
> wget http://apache.forthnet.gr/ant/binaries/apache-ant-1.7.0-bin.tar.gz


Step 2: When it is finished unpack the archive
>gzip -d apache-ant-1.7.0-bin.tar.gz
> tar -xvf apache-ant-1.7.0-bin.tar
You should end up with a folder /usr/lib/apache-ant-1.7.0/

Step 3: Lets create some Sym links again for the ant executable
>ln -s /usr/lib/apache-ant-1.7.0/bin/ant /etc/alternatives/ant
Step 4: :Lets create some Sym links for the ant home directory
>ln -s /usr/lib/apache-ant-1.7.0/ /etc/alternatives/anthome
Step 5: Lets create a sym link to the /usr/bin folder for ant
> ln -s /etc/alternatives/ant /usr/bin/ant
Step 6: Add the ANT_HOME variable like we did with java home

>cd /etc/
>vi ./profile
Step 6.1 : Add the following line
ANT_HOME="/etc/alternatives/anthome"

Step 6.2 : add ANT_HOME to the export statement
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC JAVA_HOME ANT_HOME

Restart your terminal session and now if you type:
>ant -version
Apache Ant version 1.7.0 compiled on December 13 2006

>echo $ANT_HOME
/etc/alternatives/anthome

Done!


(PS)

* I am not a linux sys admin so..maybe you can do it better..though the steps above gave me in 10 minutes a real Java and Ant setup for my CentOS 4.4 linux server.
*If you don't feel comfortable with some of the commands like the symbolic link creation or the use of vi then don't do it - I have included some links on each command!
*Yes there are some package managers that do it on their own way..I know!
*This procedure it can be applied I suppose to all the linux flavors since its not package-manager dependant
*When you see the > i just used to to show the terminal..do not write it your self most probably will be [root@localhost ~]
* I used root user to do the setup

I hope it helps!

5 comments:

  1. georgia2004@hotmail.com10:30 pm, April 30, 2007

    ksereis mipws na mou peis kai pws 8a egkatastisw to ant sta windows??

    ReplyDelete
  2. This a a great post - has helped me more than once. A definite bookmark!

    ReplyDelete
  3. I want to install JAVA SDK on my CentOS, thanks already to guide how to install.

    ReplyDelete
  4. Thanks for ant installation:)

    ReplyDelete