Setup Java ZK Framework on Tomcat

Setup Java ZK Framework on Tomcat

Status
In progress
Created
Oct 31, 2024 06:12 AM
Tags
DevOps

Introduction

The ZK Framework is an open-source Java framework for building Rich Internet Application (RIAs) with a focus of simplicity and usability. This article will guide you through the process of setting up the ZK Framework on a Red Hat Enterprise Linux (RHEL) system.

Table of Contents

Prerequisites

Ensure that you have a running instance of RHEL system.

Step 1: Install JDK

The Java Development Kit (JDK) is a software development kit (SDK) that provides tools for building, testing, and running Java applications. We need this JDK available on our RHEL system since ZK Framework is built on Java.
  1. Install the JDK
    1. Open a terminal on your RHEL machine, then update the yum package index.
      sudo yum update -y
      Install the JDK (OpenJDK 11 in this guide).
      sudo yum install java-11-openjdk-devel-11.0.20.1.1-2.el9.x86_64
  1. Configure Environment Variable
    1. You may want to set the JAVA_HOME environment variable. Open your .bashrc or .bash_profile file:
      vim ~/.bashrc
      Add the following line at the end of the file:
      export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
      Save the file and apply the changes:
      source ~/.bashrc
  1. Verify the Installation and Configuration
    1. echo $JAVA_HOME java -version
      You should see the output indicating the version of Java installed like below.
      notion image

Step 2: Install Apache Maven

Maven is build automation tool used for Java projects.
  1. Install Maven using Yum
    1. sudo yum install maven
  1. Configure Environment Variables
    1. Set the MAVEN_HOME environment variable in the same way as for JDK.
      vim ~/.bashrc
      Add the following lines.
      export MAVEN_HOME=/usr/share/maven export PATH=$MAVEN_HOME/bin:$PATH
      Save the file and apply the changes:
      source ~/.bashrc
  1. Verify the Maven Installation
    1. echo $MAVEN_HOME mvn -v
      You should see the output indicating the version Maven look like below.
      notion image

Step 3: Install Apache Tomcat

Apache Tomcat is an open-source web server and servlet. It is used widely for hosting Java-based applications on the web.
  1. Download and Extract Tomcat
    1. Navigate to the Tomcat download page at Apache Tomcat and copy the link to the latest version of Tomcat tar file. You can use the following command to download the current latest version (9.0.96) or replace it with version available.
      wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.96/bin/apache-tomcat-9.0.96.tar.gz
      Extract the downloaded archive.
      tar -xvzf apache-tomcat-9.0.96.tar.gz
      Move the extracted directory to /opt :
      sudo mv apache-tomcat-9.0.96 /opt/tomcat
  1. Set the appropriate permissions for the Tomcat directory.
    1. sudo chown -R tomcat:tomcat /opt/tomcat
  1. Navigate to the Tomcat bin directory and start Tomcat:
    1. cd /opt/tomcat/apache-tomcat-9.0.96/bin ./startup.sh
      Then, verify the installation by accessing Tomcat in your web browser.
      http://localhost:8080
      You should see the Tomcat welcome page.
      notion image
  1. Create the Service File
    1. Create symbolic link on /opt/tomcat . This way we can keep multiple version of Tomcat available on the system and refer to active on using /opt/tomcat/updated directory link.
      sudo ln -s /opt/tomcat/apache-tomcat-9.0.96 /opt/tomcat/updated
      Open a new service file for Tomcat in the /etc/systemd/system/ directory.
      sudo vim /etc/systemd/system/tomcat.service
      Add the following configuration to the file:
      [Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.1.1-2.el9.x86_64 Environment=CATALINA_PID=/opt/tomcat/updated/temp/tomcat.pid Environment=CATALINA_Home=/opt/tomcat/updated Environment=CATALINA_BASE=/opt/tomcat/updated Environment=’CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC’ Environment=’JAVA_OPTS.awt.headless=true -Djava.security.egd=file:/dev/v/urandom’ ExecStart=/opt/tomcat/updated/bin/startup.sh ExecStop=/opt/tomcat/updated/bin/shutdown.sh User=tomcat Group=tomcat UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
      Reload the systemd daemon to recognize the new service:
      sudo systemctl daemon-reload
      Start the Tomcat service
      sudo systemctl start tomcat
      If all works correctly, you should see the service status to be active (running) and you can test again to localhost:8080 .
      notion image
      notion image
      If your the Tomcat not running, you can check the logs on /opt/tomcat/updated/logs/catalina.out .
      If you encounter BindExeption: Address already in use, consider kill the process that already use the port 8080 or change the Tomcat port.
      For killing the process run:
      sudo ss -lptn 'sport = :8080' sudo kill -9 <pid>
      For changing the Tomcat port, open /opt/tomcat/updated/conf/server.xml and find this part:
      <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxParameterCount="1000" />
      Change the default port 8080 to any other that not being use.

Step 4: Create a ZK Project

  1. Create a New ZK Project
    1. Navigate to your preferred workspace directory:
      cd ~
      Use Maven to create a new ZK project:
      mvn archetype:generate -DgroupId=com.example.zk -DartifactId=my-zk-app -DarchetypeGroupId=org.zkoss -DarchetypeArtifactId=zk-archetype-webapp -DarchetypeVersion=9.6.0 -DinteractiveMode=false
      Change into the project directory:
      cd my-zk-app
      Build the project.
      mvn clean package
      This will create a my-zk-app.war file in the target directory.

Step 5: Deploy the ZK Application

  1. Copy the WAR file to the tomcat webapps directory
    1. cp target/my-zk-app.war /opt/tomcat/updated/webapps
  1. Start Tomcat
    1. sudo systemctl start tomcat
  1. Verify the Tomcat is running
    1. sudo systemctl status tomcat
  1. View Tomcat Logs
    1. If you encounter issues, view the logs:
      cat cat /opt/tomcat/updated/logs/catalina.out

Step 6: Access Your ZK Application

Open your web browser and navigate to:
http://localhost:8080/my-zk-app
You should see the default ZK application running successfully.
notion image

Conclusion

You’ve successfully created a ZK project using Maven, deployed it on Tomcat, and accessed it in your web browser.