KrungKrungXD
Posts: 7
Joined: Tue Aug 30, 2016 11:58 pm

Running a Java program with GPIO controls in startup

Wed Feb 08, 2017 8:11 pm

Hi, i have a java program that have a gpio controls. i can run the program in the terminal using (sudo java - jar path/to/my/program.jar). Now i want to run the program on startup .. anyone can help me ? thanks in advance.

The program (with GUI) is created in Netbeans IDE
Operating System installed is Ubuntu Mate
Last edited by KrungKrungXD on Thu Feb 09, 2017 9:03 pm, edited 2 times in total.

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Running a Java program with GPIO controls in startup

Wed Feb 08, 2017 8:57 pm

sudo nano /etc/systemd/system/javathing.service

Code: Select all

[Unit]
Description=javaThing daemon service
[Service]
ExecStart=/usr/bin/java -jar path/to/my/program.jar
StandardOutput=syslog
StandardError=syslog
# Don't need "sudo" if we run as root.
User=root
Group=root
SyslogIdentifier=javaThing
[Install]
WantedBy=multi-user.target
sudo systemctl enable javathing.service
sudo systemctl start javathing.service
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

KrungKrungXD
Posts: 7
Joined: Tue Aug 30, 2016 11:58 pm

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 8:17 am

Thanks for the reply, i tried your solution but I get this error "Failed to execute operation: Invalid argument" when i run this command "sudo systemctl enable javathing.service"

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 6:38 pm

Have you created the right service file in the right place?

This is what you should get in your LXTerminal window

Code: Select all

pi@viking ~ $ cat /etc/systemd/system/javathing.service
[Unit]
Description=javaThing daemon service
[Service]
ExecStart=/usr/bin/java -jar path/to/my/program.jar
StandardOutput=syslog
StandardError=syslog
# Don't need "sudo" if we run as root.
User=root
Group=root
SyslogIdentifier=javaThing
[Install]
WantedBy=multi-user.target
pi@viking ~ $ sudo systemctl enable javathing.service
Created symlink from /etc/systemd/system/multi-user.target.wants/javathing.service to /etc/systemd/system/javathing.service.
pi@viking ~ $ sudo systemctl start javathing.service
pi@viking ~ $
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

KrungKrungXD
Posts: 7
Joined: Tue Aug 30, 2016 11:58 pm

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 8:13 pm

yes I am sure that the file had been created, i looked for the javathing.service file in the /etc/systemd/system/
Here's what inside the file ..

Code: Select all

[Unit]
Description=javaThing daemon service
[Service]
ExecStart=/usr/bin/java -jar /home/pi/Desktop/dist/DPK.jar
StandardOutput=syslog
StandardError=syslog
# Don't need "sudo" if we run as root.
User=root
Group=root
SyslogIdentifier=javaThing
[Install]
WantedBy=multi-user.target

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 8:23 pm

sudo systemctl enable javathing.service
sudo systemctl start javathing.service


The way I get those commands into the system is sudo systemctl enable jav[TAB][ENTER] which lets filename completion fill in the rest of the filename.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

KrungKrungXD
Posts: 7
Joined: Tue Aug 30, 2016 11:58 pm

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 8:50 pm

still getting the same error . I am new to linux, i didnt understand what the error means . Any other advice how to run the program on startup ? I tried to add the program in "Startup Application Preference" but it doesn't work.

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 8:58 pm

You are running Raspbian Jessie aren't you?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

KrungKrungXD
Posts: 7
Joined: Tue Aug 30, 2016 11:58 pm

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 9:01 pm

No sir, I installed Ubuntu Mate on rpi3, the java program is created in Netbeans IDE.

KrungKrungXD
Posts: 7
Joined: Tue Aug 30, 2016 11:58 pm

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 9:02 pm

I guess I need to update my thread. Sorry for that

User avatar
DougieLawson
Posts: 39303
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Running a Java program with GPIO controls in startup

Thu Feb 09, 2017 9:51 pm

16.04 should have systemd unless you'd disabled it. It could be that Mate has a different place than Raspbian for user's service files.

Try creating it as /lib/systemd/system/javathing.service
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Java”