How can I use a different JDK with the Apache Tomcat AMIs?

If your application requires a different JDK than what we provide with the Apache Tomcat AMIs (Oracle JDK, IBM JDK, different JDK version), you can take the following steps to reconfigure Tomcat. (Please make sure the JDK you intend to use is supported by Tomcat.)

If you are able to use OpenJDK:

  1. Login to the instance

  2. Stop the Tomcat service by running:

    sudo systemctl stop tomcat

  3. Remove the existing OpenJDK packages by running:

    yum remove -y java-1.8.0-openjdk-headless

  4. Install the desired OpenJDK packages by running the command associated with the JDK version you need:

    yum install -y java-1.7.0-openjdk-headless
    yum install -y java-1.8.0-openjdk-headless
    yum install -y java-11-openjdk-headless
    (If you prefer the full JDK, remove "-headless" from the commands above)

  5. Start the Tomcat service by running:

    sudo systemctl start tomcat

  6. Test your instance to ensure it is still operating as expected.

  7. If confirmed to be working, these steps can be incorporated into a user-data script to automate this process.

If you require Oracle or IBM JDK:

Due to licensing restrictions, we cannot ship Oracle or IBM products with our AMIs. If you are licensed to use these products, you can deploy them to our AMIs using the following steps:

  1. Download the JDK from your vendor

  2. Copy the JDK to your EC2 instance

  3. Stop the Tomcat service by running:

    systemctl stop tomcat

  4. Remove the existing OpenJDK packages by running:

    yum remove -y java-1.8.0-openjdk-headless

  5. Install your JDK into the directory you desire. Using Oracle as an example:

    cd /opt
    tar xzf jdk-8u221-linux.tar.gz
    # Package installed into /opt/jdk-1.8.0_221

  6. Edit the /usr/lib/systemd/system/tomcat.service file and modify the highlighted lines to point to your JDK:

    [Service]
    User=tomcat
    WorkingDirectory=/opt/tomcat
    Environment=JRE_HOME=/opt/jdk-1.8.0_221/jre
    Environment=JAVA_HOME=/opt/jdk-1.8.0_221/jre
    Environment=CATALINA_HOME=/opt/tomcat
    Environment=CATALINE_BASE=/opt/tomcat
    
  7. Start the Tomcat service by running:

    systemctl start tomcat
  8. If Tomcat fails to start, examine the /var/log/tomcat/catalina.out file for error messages.

  9. If confirmed to be working, these steps can be incorporated into a user-data script (using an S3 bucket to store your JDK package) to automate this process.

Resolveu o seu problema?