Start a jar on a raspberry pi by reboot - java

i search a way to start a jar on a raspberry pi. In need to start the jar as root because the jar set gpios and this is only possible as root user.
The jar is a GUI fullscreen application. This i tried already:
Create a Crontab (contab -e, #reboot, don't work because the application is a gui application)
By adding a file called /etc/xdg/autostart/RPi-infoscreen.desktop
[Desktop Entry]
Type=Application
Name=RPi-infoscreen
Comment=Keysystem
NoDisplay=false
Exec=/usr/bin/lxterminal -e /home/pi/keySys.sh
NotShowIn=GNOME;KDE;XFCE;
The command line program is at /home/pi/keySys.sh
#!/bin/bash
cd /home/pi/Key
sudo java -jar keyTest.jar
Make it executable:
chmod +x /home/pi/keySys.sh
This had worked for me first. But than i had to change it because i need to see the exception from the terminal. So i changed the command line program what is at /home/pi/keySys.sh to:
#!/bin/bash
cd /home/pi/Key
sudo java -jar keyTest.jar 2> errorOutput.log > output.log &
Since this time nothing worked anymore. I changed it back to sudo java -jar keyTest.jar but it don't start the application anymore. I make it executable again but nothing happened.
Have someone a idea?
Thank you very much!

Related

Bash script to restart java application after Jenkins compiling

I'm noob in linux! I have my server and installed jenkins. I need to create bash script, which should run application(or will restart if it has already been started) after jenkins compile it. I tried to use screen util in linux, but it's not working for me. I wrote this script:
screen -X -S JavaTelegramBot quit
screen -d -m -S JavaTelegramBot
screen -X -S JavaTelegramBot java -jar "path/to/jar"
When I tip screen -ls, it's empty, so application not working

How do i fix this .bat file so it can run in macOS?

I have this .bat file created and working perfectly in Windows. When I tried to run this file from macOS terminal, it shows some error.
I've already had JRE installed in my Mac. I also added:
JAVA_HOME=/Library/Java/Home
export JAVA_HOME;
to my .profile file. All the jars needed are also in a folder beside the .bat file.
This is what's inside the .bat file:
shell
"%JAVA_HOME%\bin\java.exe" -cp .;libs/*;api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
I tried deleting the 'java.exe' from code above, but the error still showed up.
I expect to run this .bat file perfectly.
When it comes to setting JAVA_HOME (on macOS) it's better to use
export JAVA_HOME=$(/usr/libexec/java_home)
inside your ~/.profile. You can also pick any version you like by using -v option.
To list all JVM installations, call:
/usr/libexec/java_home -V
to select one of them, use
export JAVA_HOME=$(/usr/libexec/java_home -v version)
then, you can use it like this
$JAVA_HOME/bin/java -cp .:libs/*:api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
turning .BAT to .sh
you can also create a wrapper script like this
#!/bin/bash
export JAVA_HOME=$(/usr/libexec/java_home)
$JAVA_HOME/bin/java -cp .:libs/*:api-security-generator-0.0.1-SNAPSHOT.jar jatis.avantrade.security.securitygenerator.Main
Make sure to make it executable
chmod +x script.sh
Then, you can call it following way
./script.sh

Java not running in crontab

java -jar /home/scripts/relay.jar is working fine when I launch from command line. The command produces a file: relay.txt
In crontab
/usr/bin/java -jar /home/oneprovider/relay.jar
is not producing anything. I first had it without /usr/bin/ but then did which java and added absolute path with no luck. The jar file was originally written for windows but it works in Linux fine when launched from command line
What am I missing?
Agreed that the working directory is likely the problem. Can you write a shell script that wraps the java invocation and sets the working directory? Something like:
#!/bin/sh -e
cd /home/oneprovider
/usr/bin/java -jar /home/oneprovider/relay.jar
Then change the cron job to run the script instead. Remember to chmod it and make sure that the cron user can write to the directory if it isn't your personal crontab.

Error executing ant :-bash: ant: command not found

I am trying to deploy my application in a Linux box, I have a file called setAppPath.sh file as:
#!/bin/sh
APP_HOME=`pwd`
ANT_HOME=$APP_HOME/lib/ant
echo $ANT_HOME
PATH=$ANT_HOME/bin:$APP_HOME/scripts/unix:$PATH
echo $PATH
chmod +x $ANT_HOME/bin/ant
chmod +x $APP_HOME/scripts/unix/*.sh
export APP_HOME ANT_HOME PATH
When I try to execute ant command I get an error message as:
-bash: ant: command not found
The echo $ANT_HOME is printing my ant home location the PATH is printed properly too.
After execting setAppPath.sh file I tried echo $ANT_HOME it gave empty line.
Please help me figuring out this issue.
Edit 1: which ant give no ant
I am using sh setAppPath.sh command to execute the sh file.
When you run your script normally, what happens is that your shell starts a new process, the script runs in that process, and when the script is done the process dies and control returns to your shell.
All modifications that the script did to its environment die with it. The changes have no effect on the parent shell. Same if you're trying to run cd in a script and expecting the parent shell to move.
To run your script in the context of your shell and not in a subprocess, use the source or . commands:
source setAppPath.sh
. setAppPath.sh

Script Not Running

I have a script file that I would like to run whenever my computer starts up. What the script file does is run a .jar file that I have on my desktop.
I first created a .jar file called Hello.jar that is located on my desktop. After that I created a script file (.sh) called Script.sh that has the following contents in it.
cd Desktop;java -jar Hello.jar;
Then I followed this answer to run the file on startup. So as it says I first setup a .desktop file by running this command in the terminal.
sudo cd Desktop
sudo mv Script.sh /usr/bin
Then I did
sudo cd /usr/share/applications
sudo gedit file.desktop &
Then I wrote the following information in gedit.
[Desktop Entry]
Name=Hello.sh
Exec=/usr/bin/file.sh
Type=Application
Terminal=false
And lastly I created a copy of it in this location.
/etc/xdg/autostart/
I then restarted my computer but nothing happened.
sudo cd doesn't do anything! The cd command only takes effect within the current shell - which immediately exits!
Instead you should do sudo bash to launch a root shell. Then run all your commands within that root shell.
Also, I think you forgot to give your script execute permissions. You can do that by changing mv to install.

Categories