Jar Runner (Run java as a service)
Sometimes, you need to run java archive `jar` file without any application server. For the case, you can simply run java -jar {your jar file name}
command in your terminal. Here, I will show one of my script file to run your jar file as a service.
By using my shell script file, you will get below benefits.
- Running and hiding logs in console.
- You can run multiple java application at the same time by using this script.
- You don’t need to move this script file to your jar file path as this script can handle dynamic path.
- You don’t need to copy or duplicate this script file to run multiple jar. Just only one script file is enough.
- You can check, monitor active running java application as this script will check as per PID.
- You can restart your application by viewing running application list.
- You can stop your application by viewing running application list.
You can use my script above bash version 3, and you can check your bash version in your terminal by typing bash --version
.
- Clone the script file
git clone https://github.com/yewin-mm/java-runner.git
2. Check requirement
- Make sure java is already install in your machine, type
java -version
in your terminal to check java is installed in your machine. - Make sure your bash version is above 3, type
bash --version
to check your bash version. - Make sure your
jar
file is is able to run withjava -jar
in your local. - Please note that this script will work only in Linux based OS, not work in Window.
3. Start Service
- You can start (run) your applications by using this script.
- Go to your cloned java runner script file folder with
terminal
. - If your user have root permission, Type
sh java_runner.sh start
- If your user have not root permission, Type
sudo sh java_runner.sh start
- There, you can see this script will request the service name which you want to
run
. - In there, You can type
any name
you want and please note thatavoid special characters
in service name as my script will accept only for alphanumeric only. - After that, the script will request your
JAR
file name. - Add your JAR file name in there.
- Please note that if your JAR file is
not in the same directory
with this script, you need to type JAR file name whichincluding the valid path
. (eg. /home/app/employeeService.jar). - After that you can see the message like service is
successfully started
, and you can call your application as it's alive after starting the service. - Please note that, my script
won't generate log file
to see your application logs. - So, if you want to see your application logs, you need to generate logs file by using logging tools like log4j, etc.
4. View Services
- You can see your running applications if you run your applications by using this script.
- Go to your cloned java runner script file folder with
terminal
. - If your user have root permission, Type
sh java_runner.sh view
If your user have not root permission, Type
sudo sh java_runner.sh view
- After that you can see your running applications by list, and you can also see the total number of running services too.
5. Restart Services
- You can restart your applications if you run your applications by using this script.
- Go to this java runner script file folder with
terminal
. - If your user have root permission, Type
sh java_runner.sh restart
- If your user have not root permission, Type
sudo sh java_runner.sh restart
- You can see the running services list and total count.
- This script will request you to type the number from running services list which you want to
restart
. - Please note that you need to type
only the number
of your services as this script will accept number only. - After you type the number, the script will restart your application.
- After that you can check your application by typing
sh java_runner.sh view
.
- There you can see your restarted application is alive, and you can call your application to check your application is alive or not.
6. Stop Services
- You can stop your applications if you start your applications by using this script.
- Go to this java runner script file folder with
terminal
. - If your user have root permission, Type
sh java_runner.sh stop
- If your user have not root permission, Type
sudo sh java_runner.sh stop
- You can see the running services list and total count.
- This script will request you to type the number from running services list which you want to
stop
. - Please note that you need to
type only the number
of your services as this script will accept number only. - After you type the number, the script will stop your application.
- After that you can check your running application by typing
sh java_runner.sh view
.
- There you can see your stopped application is not listed in running services list, and it’s stopped.
- You can call your application to check your application is still alive or not.
Have Fun and Enjoy in Using my Script
Below is the Project Link for Spring-boot application with Jenkins.
Below is my GitHub profile and you can checkout all of my projects under repository tag, If you satisfied with my projects or you got some help from my projects, please help me also by giving star on GitHub and give recommendation in LinkedIn as I dropped my LinkedIn profile in below Contact Me section.
Ye Win Github — https://github.com/yewin-mm
✉️ Contact Me
Name — Ye Win
LinkedIn profile — Ye Win
Email Address — yewin.mmr@gmail.com
WhatsApp — +959252656065
Website — https://yewin.me
Project Link: Java Runner
🥰 Becoming a Sponsor
If you like any of my projects or if you want to support my work, please kindly consider becoming a sponsor. It gives me great motivation and I can relentlessly maintain my projects and contribute to the open-source community.