aspose file tools*
The moose likes Beginning Java and the fly likes Class Not Found exception in cron job Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Not Found exception in cron job" Watch "Class Not Found exception in cron job" New topic
Author

Class Not Found exception in cron job

Huw Morgan
Greenhorn

Joined: Mar 07, 2006
Posts: 23
Hi,

I am not really a beginner, but I am fairly sure this is a simple question, and I am just missing something simple.

I am running a web server on a VPS. I want to check if there are any new files on the server and if there are, add some records to a database. I have writen a java app. and a script [1] that sets the CLASSPATH and runs the app. This runs fine from the command line, but fails when run as a cron job. I have writen a little test app. that demonstates the problem [2]. The exception is at [3] with some more debugging info from the script.

The jdbc jar file is in the right place [4].

Does anyone know what is going on here? If this belongs in a different forum let me know (or just move it).

Thanks for any help,

Hugh

[1] #! /bin/sh

whoami
echo $CLASSPATH
echo $PATH
CLASSPATH=/home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar:.:/usr/local/jdk/lib/classes.zip:$CLASSPATH
PATH=/usr/local/jdk/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/usr/X11R6/bin:/home/server/bin:$PATH
echo $CLASSPATH
echo $PATH
cd /home/server/public_html
/usr/java/bin/java TestClassLoader

[2]

[3]

[4] server@imecserver1.com [~/public_html]# ll /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar
-rwxrwxrwx 1 server server 235712 Aug 7 13:17 /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar*

[ August 09, 2006: Message edited by: Huw Morgan ]
[ August 09, 2006: Message edited by: Huw Morgan ]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
I've seen this, check the permissions on the /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar and the folders leading to it. If the user that is running the Cron jobs doesn't have read permission on everything, you can get this.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Huw Morgan
Greenhorn

Joined: Mar 07, 2006
Posts: 23
As I posted in [4] above, I have put a copy of the .jar in a directory owned by the user "server", and it has all permisions (-rwxrwxrwx 1 server server 235712 Aug 7 13:17 /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar).

I have put whoami in the script, and this returns "server", so it must be that user who is running it.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
Is there a Driver class in that jar file? You sure?

Also, what gets printed for your path and classpath when you run the script from the command line?
[ August 09, 2006: Message edited by: Ryan McGuire ]
Huw Morgan
Greenhorn

Joined: Mar 07, 2006
Posts: 23
I solved the problem, I ran the app with;

java -classpath $CLASSPATH TestClassLoader

I still do not understand how this can fix it, but it will do for me.

The output of the script in in my OP. It is roughly;

#> whoami
server
#> CLASSPATH=/home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar:.:/usr/local/jdk/lib/classes.zip:$CLASSPATH
#> echo $CLASSPATH
/home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar:.:/usr/local/jdk/lib/classes.zip:

The jar file includes the driver.
Trey Brown
Greenhorn

Joined: Apr 08, 2004
Posts: 5
One thought on why your solution worked. It has been a while since I played with UNIX, but I seem to recall having a similar issue going from running a script manually to running it in the cron task. The issue, as I recall, appeared to be that the classpath was never picked up from the shell script by the java routine.

Try doing an "export" of the CLASSPATH environmental variable. I believe the issue came down to either a problem being allowed to set the environmental variable or the need for the CLASSPATH to be known outside of the shell.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1007
    
    3
Exactly what Terry said.



Without the export statements, the environment variables set in the shell script do make it into the JVM process.

If you were using bash, you could do the assignment and export in one command:
export CLSSPATH=blah blah blah

But with sh, you need the two separate lines.

From the man page for export:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Not Found exception in cron job