Java not running in crontab - java

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.

Related

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

Start a jar on a raspberry pi by reboot

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!

why doesn't jenkins start my java program?

This is extremely annoying but I have a script launch.sh like:
#!/bin/bash -x
java -jar foo.jar &
If I run it by hand, my java program starts up. However in Jenkins I configured that just does execute shell: $HUDSON_HOME/launch.sh >& out
I see from out file that it looks like it started java, but when I do a ps I don't see it there. How do I configure jenkins appropriately?
https://wiki.jenkins-ci.org/display/JENKINS/ProcessTreeKiller
add
export BUILD_ID=dontKillMe
to shell script

ArrayIndexOutOfBounds Exception on executing linux sh file

I have a program in java which takes 0'th aargument as file location like
File f = new File(args[0]);
so when i execute it using a windows batch(.bat) file it works correctly .
but when i execute the same using a linux shell file(.sh) in linux i get ArrayIndexOutOfBoundsException.
WINDOWS BATCH FILE :
#echo off
for /f %%i in ("%0") do set scriptpath=%%~dpi
set cp=%scriptpath%/../lib/*.jar;
java -classpath %cp% com.synchronizer.main.MYSynchronizer %scriptpath% "%1" "%2"
LINUX SH FILE:
export JAVA_HOME=/usr/local/java
PATH=/usr/local/java/bin:${PATH}
THE_CLASSPATH=
for i in `ls ../lib/*.jar`
do
THE_CLASSPATH=${THE_CLASSPATH}:${i}
done
java -cp ".:${THE_CLASSPATH}" \
com.synchronizer.main.MYSynchronizer
please help!
It looks like a problem in script (no arguments are passed to the Java program).
You can consider to debug the script like this: debugging scripts
Hope this helps
Your shell script is not passing any parameters:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer
Try:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer "$1" "$2"
As stated above, your Linux shell script is not sending any arguments to the Java program that you are trying to start.
And, adding to that, you are not showing us how you run the Linux shell script. If no argument is given on the command line when you start the shell script, no arguments can be passed to your Java application from the shell script.
If you want to see the actual command that is going to be run by your shell script, you can always put "echo" in front of a line and see what all variables are expanded to. This is a simple way to debug shell scripts.

how to launch java GUI test from linux

-java -classpath<> <classname> in the ".bat" file to launch java test from cmd windows
how to do that using perl to launch java test from linux ?
Don't use perl. For such a simple job, a simple shell script will do:
#!/bin/sh
/path/to/java -classpath foo.jar:bar.jar:. classname
Make the file executable with chmod +x filename and execute it with ./filename
A similar approach using the -jar option is possible. Additionally, you can forward any command line parameters using the special parameter #.
#!/bin/sh
/path/to/java -jar foo.jar "${#}"

Categories