[Linux] Auto-start a Java program (.JAR) on startup

Hi guys, Linux noob here

I have a RAspberry Pi with a samsung USB installed. I've got a custom program that is written in Java so it is stored as a .JAR file. Let's call it server.jar

This JAR file is currently residing in this path

   /media/pi/SAMSUNG/appli 

I created a autostart script in my /home/pi folder
It contains only one line

  java -jar /media/pi/Samsung/appli server.jar     

^^ I am not sure if the above syntax is correct!

I then went into rc.local as documented and added my script into the text file using nano in sudo. On reboot… nothing happens. Any idea of a way to get my JAR to autostart each time my pi reboots?

Comments

  • /etc/rc.local should run at the last stage of Linux bootstrap. However it won't send you any error if something goes wrong. First of all it's a shell script so if you want to launch something and put it in background, you need to add & at the end. Also might be a good idea to actual log something so you know what's going on. For example

    java -jar /media/pi/Samsung/appli server.jar > /var/log/javaprog.log 2>&1 &
    

    to the rc.local file. So if your Java program does not get started you can check the log file.

  • hmmm. It's really odd — I executed my updated script (using right click -> Open -> Execute) but nothing happens.

    Log file also not created.

    There must be something I'm missing

  • I think that if you use java -jar the class to run has to be in the manifest inside the jar
    you could run java -cp server.jar <classname>

    • Yeah the rabbit hole is getting deeper now

      I will have to ask the publisher of the program to see how exactly I'm supposed to start it using a script.

      Thing is if I cd directly to my Samsung directory and run the below in the Terminal, it starts right away

       java -jar server.jar
      

      but Scotty's command does nothing when executed in my .sh script.

      • Wouldn't you then use "java -jar /media/pi/Samsung/appli/server.jar" ( note "/" instead of space

  • +1

    you should be able to add a cron job with the scheduled time '@reboot'

  • chmod +x foo.sh?

    • Did that. Nothing happens when I type that command into Terminal though. How do I check that it has the proper permissions?

      Anyway I wrote a service wrapper for it as according to instructions here

      http://www.jcgonzalez.com/linux-java-service-wrapper-example

      but when I attempt to test it, it says

      Failed to start myserver.service: Unit myserver.service failed to load: No such file or directory

      $!@#% Linux
      (bangs head on brick wall)

Login or Join to leave a comment