Tomcat server

How to start Tomcat server?

Step 1: Download Jakarta Tomcat from the Tomcat home page, and get the binary version - I_downloaded jakarta-tomcat-5.0.27.tar.gz.

Step 2: Extract the files into a directory of your preference. e.g. Documents

Step 3: Change the permissions of the “.sh” files in the bin directory using chmod +x *.sh

Step 4: Run this to start the server and tail the logs:

./startup.sh && tail -f ../logs/catalina.out

How to deploy applications in Tomcat?

Build war files and copy them into apache-tomcat-9.0.78/webapps directory.

And then, restart the server.

How to set custom JVM arguments for applications when working with Tomcat server?

For Tomcat 7 and above you should set the parameters in the {Catalina Root}/bin/setenv.sh like such:

export CATALINA_OPTS="$CATALINA_OPTS -Dapp.username=username -Dapp.password=password"

NOTE: Notice the $CATALINA_OPTS at the beginning so you don’t wipe out any previously set values. Not doing so can create a very hard to debug problem!

If the parameters you are setting are solely to be used by Tomcat then be sure to set it using CATALINA_OPTS.

If your application will be using the parameters then be sure to use JAVA_OPTS instead. Tomcat will also read these parameters. This can also go in the setenv.sh file.

For instance:

export JAVA_OPTS="$JAVA_OPTS -Dapp.username=username -Dapp.password=password" -DMICROSERVICES_CONFIG=/Users/explorer436/microservices-config

Helpful script

Before running this script, make sure that the war file is built with the expected version of jdk using maven toolchains.

cd /Users/explorer436/Documents/apache-tomcat-9.0.78/bin
./shutdown.sh

cd <location of the application>
mvn clean install
cp target/application.war /Users/explorer436/Documents/apache-tomcat-9.0.78/webapps

cd /Users/explorer436/Documents/apache-tomcat-9.0.78/bin
./startup.sh jpda start && tail -f ../logs/catalina.out

Remote debugging

In {Catalina Root}/bin/setenv.sh, add CATALINA_OPTS

Before you set these, make sure that there are no jdwp settings in your existing setup. Tomcat doesn’t work well with duplicate settings.

# Change this port number based on the port number that the application is using.

# Note: The port number that is used in this command has to be different from the one the application is using.
# The reason is, the running application and the debug session are two separate processes.
# They will work together- but they are separate processes nevertheless.

## If you have existing CATALINA_OPTS
export CATALINA_OPTS="$CATALINA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

## If you don't have existing CATALINA_OPTS
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

And whereever you are starting it from (using the script above), make the change to use this command.

./startup.sh jpda start

How do I remotely debug Tomcat using Eclipse?

  1. Go to “Run->Debug Configurations…”.
  2. Click on “Remote Java Applications”, then click “New”.
  3. Give this configuration a name using the title box.
  4. Save and run.
  5. Eclipse will connect to the JVM that Tomcat is running under.

How do I remotely debug Tomcat using IntelliJ?

  1. Go to “Run->Edit Configurations…”.
  2. Add “Remote JVM Debug”, specify Host (localhost) and Port (8020).
  3. Give this configuration a name using the title box.
  4. Save and run.
  5. (If necessary, paste these in “Command line arguments for remote JVM”: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8020)
  6. IntelliJ will connect to the JVM that Tomcat is running under.

Links to this note