Cron job java


#1

I made a cron job via panel to run a .jar file.

I added this in commands to run:

/usr/bin/java -jar /home/myuser/myprogram.jar

and i got this error:
Exception in thread “main” java.lang.NoClassDefFoundError

It works fine when i run it via SSH but it doesn’t when i try via cron job.

Does anybody know how i can do it?


#2

That error would seem to indicate that the jar file you’re trying to run is incomplete, or isn’t meant to be run directly. What program is it?


#3

It’s a program i made in java. There is no problem with the jar file because i can run it normally via SSH and it’s working fine. It just don’t work when i try to run it via cron job.


#4

You may be missing some key environment variables when running via cron. Type ‘env’ while logged in via ssh and look for JAVA specific variables, especially CLASSPATH.

Then add those to your crontab as an export before executing the app.

e.g.

export CLASSPATH=$CLASSPATH:/what/ever
/usr/bin/java -jar /home/myuser/myprogram.jar
[hr]
Hmmmm…actually, the export should go on the same line as the exec:

export CLASSPATH=$CLASSPATH:/what/ever && /usr/bin/java -jar /home/myuser/myprogram.jar

Or, place all that in a .sh script and run the script from cron