, ,

How to Deploy a JAR program, Optimize, Secure, and Schedule Auto-Run

Posted by

In Java when we build a program the output is a JAR(Java ARchive) which is the compressed format used in Java Programming to be able to package java applications and applets, as well as libraries, and dependencies used by the applications and applets. execution of JAR File, can be done directly using JVM(Java Virtual Machine) without having to install applications on each machine individually

Deploy and AutoSchedule Run of JAR File

For us to deploy the JAR File and auto schedule to run in the time we want, for windows we will use Task Scheduler and for Linux, we use the cron tool

Steps Deploying a Java JAR file to automatically run on Linux

For us to run on Linux it involves making changes to the cron table to add a new entry to auto-schedule the JAR file at the set time. The steps are listed below.

  1. Open the terminal and type “crontab -e” to open the cron table file using the default text editor.
  2. Add a new line at the bottom of the file with the following format:
    • *****java -jar /path/to/your.jar
  3. Save and exit the cron table file. It will trigger the JAR file to run every minute.

Example of how the crontab file looks:

# Edit this file to introduce tasks to be run by cron.
#
# Each line of this file represents a single cron job to be run. 
#
# To schedule the job "my-job" to run at 2:15 PM every day, the line should read:
# 15 14 * * * /path/to/my-job
# 
* * * * * java -jar /path/to/your.jar

Steps Deploying a Java JAR file to automatically run on Linux

WIth Windows it involves the process involves creating a new task in the Task Scheduler to run the JAR file at a specified time. Here are the detailed steps:

  1. Open the Task Scheduler by going to Control Panel > Administrative Tools > Task Scheduler.
  2. On the Task scheduler Window Click on “Create Basic Task” in the right-hand pane.
  3. Spwcify the name of the file in the “Name” field, enter a name for the task, such as ” o mins Run JAR file”.
  4. Once done naming In the “Trigger” section, specify the time you would like to be running the task.
  5. Proceed to Specify the path to the “Java.exe” and the JAR file arguments then In the “Action” section and choose “Start a program”.
  6. Complete the Setup by Clicking on Finish to save the task.
Task Scheduler

Deploying a Java JAR file to automatically run on using shell script

We can run a JAR file automatically by creating a shell Script then proceed to schedule using the “cron” tool for Linux and “Task scheduler” for windows.Listed below is a script to enable us run the JAR file.

#!/bin/bash

java -jar /path/to/your.jar

What is required of us is to ensure you save the script with a .sh file extesion and should be assigned the rights as executable using chmod+x Myscript.sh and be aded in the crontab

We have build tools that allows us to build the “JAR” file including all its dependancies,this will allow the JAR file be runned using a sibgle command example java -jar myscript.jar with no worries of having to set up the classpath and installing the dependancies.

Deploying a Java JAR file to automatically run on using containerization technology -Docker.

Docker is a containirization technology that llows us to package JAR files and the dependancies into a container that can be runned on any machine that docker is installed

Steps to Deploy a JAR File using Docker

  1. Create a Dockerfile that specifies the base image to use, copies the JAR file into the container, and sets the command to run the JAR file.
  2. Build the Docker image using the command docker build -t my-jar-image .
  3. Start a container from the image using the command docker run my-jar-image
  4. You can also schedule the container to start and stop at a specific time using the docker start and docker stop commands.

Kubernetes is a tool that provides us with a framework for running and scaling containers,other third party services include apahe Mesos,Amazon Elastic Container Services(ECS) to manage and schedule the containers.

Another way to schedule the container is to use the tool like Kubernetes, which provides a framework for running and scaling containers across multiple machines. You can define a Kubernetes CronJob resource that runs the container at a specified time, and the Kubernetes control plane will automatically create and delete pods to run the container.

It is recommended that you always design the JAR file to run as a service so that it does not finish the execution process and stops

Example of a Dockerfile that copy the JAR file into the container and sets the command to run the JAR file

FROM openjdk:8
COPY my-jar-file.jar /app/my-jar-file.jar
CMD ["java", "-jar", "/app/my-jar-file.jar"]

How to Secure a JAR file to protect Application and Data

There are measure that are important to implement to ensure the JAR files are protected from malicious attacks and people not authorized to access are locked out,this are some of the measures:-

  1. Use strong authentication and access controls
  2. keep your JAR files and any dependencies up-to-date
  3. Sign JAR files with a digital signature to ensure that they have not been tampered with.
  4. Ensure that any sensitive data, such as passwords or private keys, are encrypted and stored in a secure location.
  5. Use a firewall to restrict incoming and outgoing traffic to and from the JAR files and the servers where they are deployed.
  6. Ensure that the file system where the JAR files are stored is properly secured.
  7. When the JAR files need to communicate with external systems, make sure to use a secure communication protocol such as HTTPS or SFTP.
  8. When the JAR files communicates with internal systems, consider using a VPN to create a secure connection between the systems.
  9. Limit the number of people who have access to the JAR files and the servers where they are deployed.
  10. Consider using a containerization technology like Docker or Kubernetes to deploy your JAR files so as to isolate the JAR files and the resources they need to run.
  11. Use a Security Information and Event Management (SIEM) system to help identify and alert on any potential security threats.
  12. Regularly backup your JAR files and keep them in a secure location.

Conclusion

The advantages of using a JAR File include portability it can run across different Operating systems,it is easy to distribute as you just share and install the Java application,also it has enhance security since we can sign to verify the authenticity of the file.WIth Java applications packaging apps for distribution can be in the form of a WAR(Web Archives) file based on the types of application.

Related Content

https://www.advancelearnlinux.com/top-10-books-in-amazon-to-learn-java-programming/
Java Programming

One response

Leave a Reply

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