Sunny Sunny - 5 months ago 27
Bash Question

Crontab java.lang.ClassNotFoundException with a jar file

#!/bin/bash
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk
CLASSPATH=/Users/sunny/CronTest/out/production/CronTest
$JAVA_HOME/Contents/Home/bin/java -cp $CLASSPATH ".:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar" sample.Main
exit 0


Sqlite jar file is in
/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar


Compiled java class file is in
/Users/sunny/CronTest/out/production/CronTest/sample/Main.class


And I've set the cron job to be schedule at every 1 minute. Java class is getting exucuted but I am getting
java.lang.ClassNotFoundException: org.sqlite.JDBC


Same command which is in script is working in terminal.

My question is how can I add jar file properly to be executed from shell script.

Answer

Seems there is a space in between your $CLASS PATH and ".:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar" and missing a colon(:).

Please try with following with export:

export CLASSPATH=.:/Users/sunny/CronTest/out/production/CronTest:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar

and call java command with -cp $CLASSPATH.

If it still not work, please try with -cp directly:

$JAVA_HOME/Contents/Home/bin/java -cp ".:/Users/sunny/CronTest/out/production/CronTest:/Users/sunny/Downloads/sqlite-jdbc-3.8.11.2.jar" sample.Main
Comments