How to run Java application on startup of Ubuntu Linux

14,552

Solution 1

Ideally you should create a service wrapper for your java application and then make this service run on startup example here.

Use
sudo update-rc.d mytestserv defaults to run your service wrapper on startup on Ubuntu

Solution 2

So two things you'll need to do:

First create a small shell script to start your java program from a terminal. As you have packaged as a jar have a look at this, specifically the JAR Files as Applications section.

This may be sufficient: (although you'll want to use the full path to Java)

#!/bin/bash
java -jar path_to_jar_file

You should be able to run your script and successfully start your program.

Once you've got it starting from a script you can use standard linux tools to start the script. Either putting it in /etc/rc.local, or as you're using Ubuntu, use update-rc.d to start it on boot. See here for a very simple example of using update-rc.d

Hope this helps,

Will

Share:
14,552
Ram
Author by

Ram

Updated on June 12, 2022

Comments

  • Ram
    Ram almost 2 years

    I have a requirement where I need to develop application that reads TCP/IP Socket. I successfully made the program as Java program with No GUI means as soon as program runs it starts listening to the Socket and gets the response with the help of Netbeans IDE. Now as per my requirement i have to start execution of this program as soon as Linux system Booted.

    Actually I am very novice in Java and Linux Platform, so have few doubts..

    1. Is my Socket Program with no GUI is fine to be run as per my requirement.
    2. How can I write script to run jar on Linux Boot up, I got to know. init.d is meant for this.
  • Ram
    Ram about 10 years
    What if i wont change PID_PATH_NAME=/tmp/MyService-pid in your program.If i let it be as it is.Will it be OK
  • Ram
    Ram about 10 years
    one More Silly question.Am sorry sir i am very novice to Linux ..How to come out of this command sudo vi /etc/init.d/mytestserv after pasting the script and execute next command i.e sudo chmod +x /etc/init.d/mytestserv
  • PbxMan
    PbxMan about 10 years
    you mean getting out the vi editor!!?? press "ESC + :wq + "INTRO" to exit. If you mean to reboot the machine try "sudo reboot now" or "sudo reboot"
  • Ram
    Ram about 10 years
    Got it sir..One More thing if my service is in started Mode and after some time i rebooted my Linux System then My Service will automatically be Started.Am i Right?
  • PbxMan
    PbxMan about 10 years
    Yes Reboot you computer if you can to double check it. Lot's of luck!