After the installation of Java on a different OS, it's now time to set the environment variables for Tomcat at the OS level. In order to run Tomcat, we have to define the JAVA_HOME
as an environment variable and set the path for Java so that it can be accessed from any partition of OS.
JAVA_HOME
is the JDK install directory, for example, C:\jdk6
. It is meant to be set as an environment variable and referenced in Windows batch files or Unix scripts. In Tomcat, JAVA_HOME
is defined in catalina.sh
under TOMCAT_HOME/bin
. The following screenshot shows the definition of JAVA_HOME
in Tomcat 7. Once you execute startup.sh
, it internally calls catalina.sh
and invokes JAVA_HOME
. In a production environment, it is always recommended to use the permanent environment variable.
Setting the global path variable in Windows
After setting JAVA_HOME
, now it is time to add the Java path in the global path variable. Following is a detailed procedure, which needs to be followed for creating the global path variable in Windows:
1. Right-click on the My Computer icon on your desktop and then click Properties.
2. Click Advanced | Environment Variables.
3. Under System Variables, click on Path.
4. Edit the path and add the Java path in the end.
5. In the Variable value field, enter the installation path of the Java Development Kit, as shown in the previous screenshot.
6. Click OK.
7. Click on Apply Changes.
Setting the JAVA_HOME and the PATH environment variable in Linux
The environment variable and path are set differently in Linux as compared to Windows. Perform the following steps to set the environment variable in Linux:
1. Open the .bash_profile
using the vi editor for the root user.
Note
You can put the environment variable in bashrc
also. It will also execute at the time of the user login.
2. Add the following environment variable in the file. The following is the code snippet of .bash_profile
. The highlighted code shows the declaration of JAVA_HOME
and PATH. export
will add the JAVA_HOME
and PATH
to the system parameter for every user login.
3. Save the .bash_profile
using the :wq
command.
4. Once you have saved the .bash_profile
, then you have to logout and re-log in to the environment to activate the changes using the following command:
Note
If we run the previous command for any user, then the profile of that user will be reloaded.
Also, you can run the env
command to verify the environment variables are configured properly, as shown in the following screenshot:
Note
It is always best practice to first take the backup of the existing profile. In case there are issues while doing the changes, then we can revert back the changes using the command cp
Now we have set the environment variable for Windows and Linux environments, but how can we verify whether the environment is set properly or not?
Before we start installation of Apache Tomcat 7, let's quickly verify the environment variable on both the OSes.
In the Windows environment, variables can be verified using the following command:
For JAVA_HOME:
For PATH:
In Linux, we can use the following command to verify the environment variables:
For JAVA_HOME:
For PATH:
After verifying the environment variable on both the OSes, we are sure that JAVA_HOME
and PATH
are properly set in the environment. We have completed the prerequisites of installation of Apache Tomcat 7. Now, we can proceed with the installation of Apache Tomcat 7.