Installing Java and Apache Tomcat on Ubuntu

 

In order to Install Java and Apache Tomcat on Ubuntu:

  • Make sure that Java and Apache Tomcat is not present(running) on Ubuntu.
  • Use following command to check whether Java is present or not on Ubuntu.

 

Java –version

If you get result similar to the below image you do not have Java installed. The list shows you which packages in your system have Java available and you have to install it.

chkjava

  • Next You can check for Apache tomcat is present or not on Ubuntu using commands

ps –ef | grep tomcat
curl 127.0.0.1:8080
telnet 127.0.0.1 8080

 

Installation Steps:

1)  You can download it through apt-get ,type the following command at the prompt and Press Enter :

sudo apt-get install tomcat7

2)  To download tomcat from their site, copy the link for the tar.gz package under the “Core” section and begin the download. You will get a link that originates from one of Apache’s many mirrors, making the command look mostly like this (although coming from a different site).

wget http://apache.mivzakim.net/tomcat/tomcat-7/v7.0.57/bin/apache-tomcat-7.0.57.tar.gz

3)  After the download completes, untar the file.

tar xvzf apache-tomcat-7.0.57.tar.gz

4)  Finish up the Tomcat installation on the VPS by moving the files to a convenient directory.

sudo mv apache-tomcat-7.0.57 ~/path/to/tomcat

5)Check for Java Installation

a)Login to Ubuntu
b) Check if Java is not already installed or not using command
java -version

Installing Default JRE/JDK:

c) If it returns “The program java can be found in the following packages”, Java hasn’t been installed yet, so execute the following command:
sudo apt-get install default-jre

d) This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), which is usually needed to compile Java applications, execute the following command:
sudo apt-get install default-jdk

e) That is everything that is needed to install Java.

Installing OpenJDK 7:

f)To install OpenJDK 7, execute the following command:
sudo apt-get install openjdk-7-jre

g)This will install the Java Runtime Environment (JRE). If you instead need the Java Development Kit (JDK), execute the following command:
sudo apt-get install openjdk-7-jdk

h)To set the JAVA_HOME environment variable, which is needed for some programs, first find out the path of your Java installation:
sudo update-alternatives –config java

It returns something like:
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status:
* 0 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 auto mode
1 /usr/lib/jvm/java-6-openjdk-amd64/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-7-oracle/jre/bin/java 1062 manual mode

Press enter to keep the current choice[*], or type selection number:
The path of the installation is for each:

1. /usr/lib/jvm/java-7-oracle
2. /usr/lib/jvm/java-6-openjdk-amd64
3. /usr/lib/jvm/java-7-oracle

i)Copy the path from your preferred installation and then edit the file /etc/environment:
sudo nano /etc/environment

j)In this file, add the following line (replacing YOUR_PATH by the just copied path):
JAVA_HOME=”YOUR_PATH”

That should be enough to set the environment variable. Now reload this file:
source /etc/environment

k)Test it by executing:
echo $JAVA_HOME

If it returns the just set path, the environment variable has been set successfully. If it doesn’t, please make sure you followed all steps correctly.

6)In order to start Tomcat, we need to add it as an environment variable in the /.bashrc file.
sudo nano ~/.bashrc

7)You can add this information to the end of the file:
export JAVA_HOME=/usr/lib/jvm/default-java
export CATALINA_HOME=~/path/to/tomcat

Save and exit out of .bashrc. You can make the changes effective by restarting the bashrc file.
. ~/.bashrc

8) Tomcat is now installed and configured on our virtual servers. However, it is not yet activated.
The final step is to activate Tomcat by running its startup script:

$CATALINA_HOME/bin/startup.sh
Once that runs, Tomcat is up and ready on port 8080.

-Sayali Mahale

Leave a Reply