Thinker
Thinker

Reputation: 6892

ClassnotFoundException while running a java class through crontab using unix?

I have written a shell script which has a Demo class having main method.

Now when I am running my shell using ./file.sh .It works fine.

But now I have configured a cronjob to execute that file every 5 mins using crontab.

But this is giving me a classnotfoundException

Exception in thread "main" java.lang.NoClassDefFoundError: com/example/Demo
Caused by: java.lang.ClassNotFoundException: com.example.Demo
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Met

Here is my shell file.

#!/bin/sh

echo 'Starts'
lib1='HitURL.jar'

CLASSPATH=$lib1
java -cp HitURL.jar:. com.example.Demo http://www.google.com
echo 'Ends'

How i can remove this error?

Thanks.

Upvotes: 2

Views: 3298

Answers (1)

Liv
Liv

Reputation: 6124

I would bet good money that the problem is to do with the fact that when you run that script by hand you run it from the directory where HitURL.jar is -- whereas when cron runs the process, it would probably use the root directory as the current directory. As such I'd change it to include the full path to HitURL.jar in the lib1 variable -- then make sure you also reference it in the -cp parameter something like this:

#!/bin/sh

echo 'Starts'
lib1='/full/path/to/HitURL.jar'

CLASSPATH=$lib1
java -cp $lib1:. com.example.Demo http://www.google.com

Upvotes: 4

Related Questions