6

First of all I am a newbie for Linux. And as I mentioned at the title of my question, I want to execute a jar file, when Raspberry boots up. I have Raspbian OS in my Raspberry PI device. Thanks for help.

1
  • non gui(swing) based jar are only executable from rc.local or crontab. Mar 27, 2018 at 18:08

4 Answers 4

11

take a look at

/etc/rc.local

for system wide configuration

or

~/.bashrc or 
~/.profile.

for user driven configuration

furthermore you can do this:

create two files

    Start Script: /usr/local/bin/jar-start.sh
    Stop Script: /usr/local/bin/jar-stop.sh

start script:

#!/bin/bash

java -jar myapp.jar 

stop script:

#!/bin/bash
pid=`ps aux | grep myapp | awk '{print $2}'`
kill -9 $pid

Create the following script (myapp) and put it on /etc/init.d.

put this script in

/etc/init.d

content:

#!/bin/bash
# MyApp
#
# description: your app description

case $1 in
    start)
        /bin/bash /usr/local/bin/jar-start.sh
    ;;
    stop)
        /bin/bash /usr/local/bin/jar-stop.sh
    ;;
    restart)
        /bin/bash /usr/local/bin/jar-stop.sh
        /bin/bash /usr/local/bin/jar-start.sh
    ;;
esac
exit 0

now to start after boot type

update-rc.d myapp defaults 

..i know upstart. but i like SysV ;)

---- edit ----

x session: Reference and Tutorial

you have to make sure your .xinitrc contains: java -jar jarname.jar

detailed explanation:

Graphical Application without desktop environment

The 3-step tutorial will only start your application without a "window-manager" or bottom toolbar, the only way to get a bottom bar is to explicitly start it. Mouse and keyboard input will work ok without a window-manager but there is no way to move the application windows unless one is started.

If you want to move window border you also need to start a "window-manager" in parallel to your application. The .xinitrc will then look like this:

#openbox-session is the lxde window-manager
# commands ending with & will not wait until the command is exited,
# & allows the openbox-session and your application to be run in parallel
openbox-session &
java -jar jarname.jar
4
  • 1
    Note you won't be able to start a swing app this way because it requires an X session.
    – goldilocks
    Jan 17, 2014 at 20:22
  • @mft solved your answer?
    – Alex Tape
    Jan 18, 2014 at 12:26
  • @goldilocks Some explanation would be great about X session. I googled it but couldn't find so much about it. any useful link ?
    – mft
    Jan 20, 2014 at 8:11
  • @mft updated my answer
    – Alex Tape
    Jan 20, 2014 at 11:00
2

A much easier way than setting up rc.local scripts with the Pi is to use crontab.

Here's a standard example of a crontab line:

0 0 * * * root service ssh restart

The first five entries give a time (the 0th minute of the 0th hour of any day of any month of any year), the next entry (root) tells which user to run the command as, and all after that is the command to run. So this example restarts the ssh server every day at midnight.

But with Raspbian, you can use times like '@reboot'. Here's a line I use for running a python script at startup as the user pi:

@reboot pi python /home/pi/scripty.py

You would replace the python /home/pi/scripty.py with whatever command you use to run your jar file. (Note that it's generally good practice to put in the full path to any files in your home directory.)

1

Using crontab:

In order to choose which user is going to run the command, I had to create a crontab for the specific user. The way @sdenton4 suggested didn't work for me.

So to run a jar on boot with the user "pi" I had to run "crontab -u pi -e", and it looks like this:

pi@raspberrypi~$ crontab -u pi -e
no crontab for pi - using an empty one

After that it's straightforward, paste the command with full paths.

@reboot /usr/bin/java -jar /home/pi/myapp.jar

Once crontab is created for pi "-u pi" is no longer needed to edit. You would only have to run "crontab -e" logged in as "pi".

I know it's been a while since the question was asked...Hope this helps someone. cheers

1
  • For the last Answer you should change: @reboot /usr/bin/java -jar /home/pi/myapp.jar To: @reboot sudo /usr/bin/java -jar /home/pi/myapp.jar Mar 26, 2016 at 8:34
0

I'm assuming that you already install Java environment.

If you want to run as a system user, you need to call you jar in /etc/rc.local. Or you want to run as a common user, you will use ~/.bashrc or ~/.profile.

Some references to help you:

[1] https://wiki.debian.org/DotFiles

[2] http://www.debian.org/doc/manuals/debian-faq/ch-customizing.en.html

Not the answer you're looking for? Browse other questions tagged or ask your own question.