Auto Running A Java Script On Raspberry Pi

We are trying to automatically run a Java GRIP jar on a Raspberry Pi when it boots up to run our vision tracking process. Does anyone know how you do this or how your team does this?

Generically, you are looking for rc.local. In here you will want something like:

java -jar myjar.jar &

Don’t have a pi laying around at the moment, but you might want/need to use the full path to java in rc.local as well (something like /usr/bin/java). If java works when you are logged in, find its location with this command:

which java

See here for information on configuring your manifest file to make a jar runnable.

Your GRIP jar, did you create it using code generation or are you trying to run GRIP itself and load your file into it?

You’re going to have to write a Systemd service file. Systemd is what the rPi uses as its init system, which is what runs things when the system boots up, logs in, etc.

First, you’re going to want to make a new shell script to launch the vision tracking thing. Put it in your home directory (/home/raspberry most likely), and call it launchtracking.sh.
The first line should be #!/bin/sh, and the rest should be the necessary code to launch your vision tracker (probably just something like[FONT=“Courier New”] java -jar /home/raspberry/vtrack.jar). The #!/bin/sh specifies the shell interpreter to use when running the rest of the script.

Then, run chmod a+x /home/raspberry/launchtracking.sh. That’ll allow the script to be directly executable, which is necessary for the next step.

Next, you’re going to want to make a new service file. I don’t remember off the top of my head where those are stored on rPis. It’s probably something like /lib/systemd/system.
You can go to root (cd /) and run the command sudo find -name *.service, which should list any service files it finds while scanning the filesystem. Don’t mind any permission denied notices, it just has to do with how the linux filesystem works. With any luck, the preincluded system service files will show up, and you’ll know where they are.

Assuming that /lib/systemd/system/ was the correct path (if not, just substitute it with the correct one), you’re going to want to make the file /lib/systemd/system/visiontracking.service. Then, add the following contents:

[Unit]
Description=Vision tracking service

[Service]
ExecStart=/home/raspberry/launchtracking.sh
StandardOutput=null

[Install]
WantedBy=multi-user.target
Alias=visiontracking.service

Next, simply do sudo systemctl enable visiontracking.service to set to autostart at boot, and sudo systemctl start visiontracking.service to start the tracking script without rebooting.

This is, of course, assuming that the Raspberry Pi actually uses Systemd and I’m not just misremembering. If it doesn’t, just add /home/raspberry/launchtracking.sh to /etc/rc.local

Thank you to everyone who responded. We figured it out!

Might be of help to future searchers: https://github.com/WPIRoboticsProjects/GRIP/wiki/Running-GRIP-on-a-Raspberry-Pi-2

Also, because I’m talking to the future and I can only assume that the reptile overlords have overtaken the Earth… you can identify them by their love for pineapple on pizza.

While this was a quite useful and informative page (and served 1319 well in 2016) any future readers should be sure to note the disclaimer in this page now:

At this time (13 Dec 2016), these instructions do not result in functional code.

Good catch.

You should use GRIP to generate code you can use in this project template:

Which is what 1319 ended up doing for 2017.

For some reason we struggled with putting the pieces together, being new to Gradle and not being Java experts, but we were eventually able to make it work.