How Do You Get Apache Tomcat to Work from the Source Installation Media?

Problem scenario(s)
One of the following problems apply.

1. You are trying to start Tomcat on Linux (e.g., with bash catalina.sh start) and you get this error:

java.lang.ClassNotFoundException: org.apache.catalina.startup.Catalina
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at org.apache.catalina.startup.Bootstrap.init(Bootstrap.java:262)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:444)

* OR *
2. You have nothing installed on Linux yet. You want to install Apache Tomcat from the source (not binary) installation media here. What should you do?

Solution

Prerequisite
Install Java. If you need assistance, see this posting.

Procedures
1.a. Go here: https://tomcat.apache.org/download-90.cgi
Look under "Source Code Distributions" to find the link for the .tar.gz file.

1.b. Download it to the Linux server. It will be something like this:
curl -k http://ftp.wayne.edu/apache/tomcat/tomcat-9/v9.0.22/src/apache-tomcat-9.0.22-src.tar.gz > /tmp/apache-tomcat-9.0.22-src.tar.gz

1.c. Copy the installation media to the /opt/ directory. Here is an example command:
sudo cp /tmp/apache-tomcat-9.0.22-src.tar.gz /opt/

1.d. Expand the content in /opt/. Here are some commands to possibly help you with this:
cd /opt/
sudo tar -zxvf apache-tomcat-9.0.22-src.tar.gz
sudo mv apache-tomcat-9.0.22-src tomcat9

2.a. The catalina.sh file in the "bin" subdirectory of the directory just created from the "tar" command should have certain settings. It needs to be executable by the user that will start the Tomcat service. You may need to use the chmod command accordingly.

2.b. If you know the JRE_HOME variable setting, skip this step. To mentally determine the JRE_HOME directory run this long command:

which java | xargs ls -lh | awk '{print $NF}' | xargs ls -lh | awk '{print $NF}' | xargs ls -lh | awk '{print $NF}' | xargs ls -lh | awk '{print $NF}' | xargs ls -lh

Take the file path that results from the above command and remove the "bin/java" part. The resulting string is what the JRE_HOME environment variable should be, but it can be desirable to set it in the catalina.sh file exclusively (and not set it as an environment variable).

2.c. Modify the …/tomcat9/bin/catalina.sh file so one line (we recommend line 154 if you are not sure) so it has this JRE_HOME assignment. As of August 14, 2019 with Tomcat version 9.0.22 it will be immediately below a comment line in the catalina.sh file that has as this text:

# For Cygwin, ensure paths are in UNIX format before anything is touched

The line that gets this assignment (as of 8/14/19 it is line 154) should have the appropriate JRE_HOME value. (In this example of the assignment, the "/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java" is the result of the command run in 2.b. above):

JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre/

Notice the "bin/java" was truncated from the assignment above. As we know this catalina.sh file may change, we know the assignment cannot be too high (early) or too low (late) in the script for Tomcat to start properly. Here are line numbers and a line above and below the stanza to give you context:

154 # For Cygwin, ensure paths are in UNIX format before anything is touched
155 JRE_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre/
156 if $cygwin; then

3. Create a "temp" directory in the Apache Tomcat directory (e.g., /bin/apache-tomcat-9.0.22.src/temp/ or /opt/tomcat9/temp). It will be a sibling directory of the "bin" directory as referred to earlier. Modify the "temp" directory so it has very open permissions. We recommend this on the newly created file (but for security reasons you may not be allowed to do this): sudo chmod 777 temp

4. In the Apache Tomcat directory (e.g., /opt/tomcat9/ or /bin/apache-tomcat-9.0.22.src) create a lib directory. Here is how you would do it: mkdir lib

5. In the Apache Tomcat directory (e.g., /opt/tomcat9/ or /bin/apache-tomcat-9.0.22.src) create a logs directory. sudo mkdir /opt/tomcat9/logs
Give it 755 permissions with this command: sudo chmod 755 /opt/tomcat9/logs

6. Make sure the permissions on this directory allow the user that will start the Tomcat service (e.g., run the catalina.sh script) can read the files therein. Use chmod accordingly.

7.a. Download the binary media for Tomcat for the exact same version of Tomcat (e.g., 9.0.22). You will still use the source installation media primarily. But you need some .jar files. The binary installation media can be found here (in a .tar.gz file) under the "Binary Distributions" heading and under the "Core" subheading. Once you download the .tar.gz file to the Linux server, use tar -zxvf nameOf.tar.gz to extract the contents. Freely change the permissions on the folders thus created as you will delete them (e.g., once you change directories to be inside the newly created directory of extracted files, run a command like this chmod 777 lib && chmod 777 bin).

7.b. Retrieve the .jar files in the lib directory of the binary installation media and place them in the lib directory created in step #4. Take all the .jar files from the lib directory therein and copy them to the lib directory in the Apache Tomcat directory (e.g., /opt/tomcat9/lib/ or /bin/apache-tomcat-9.0.22.src/lib/).

These are suggested commands:
sudo mkdir /opt/tomcat9/lib
cd lib # where lib is the directory that was extracted with step #7.a.
sudo cp -r * /opt/tomcat9/lib

7.c. Go to the "bin" subdirectory of the files expanded from the binary installation media (the .tar.gz file). Copy "bootstrap.jar" and the "tomcat-juli.jar" to .jar files and place them in the "bin" subdirectory referred to in step #2.a.

These are suggested commands:
sudo cp tomcat-juli.jar /opt/tomcat9/bin/
sudo cp bootstrap.jar /opt/tomcat9/bin/

7.d. Delete the directory extracted from 7.a.

8. Now start Tomcat (e.g., with sudo bash /opt/tomcat9/bin/catalina.sh start). Find the Linux server's external IP address if you do not know it with this command: curl http://icanhazip.com

9. Craft a URL like this: http://x.x.x.x:8080 where x.x.x.x is the Linux server's IP address. Go to this URL from a web browser to test out your Apache Tomcat installation.

Leave a comment

Your email address will not be published. Required fields are marked *