Class Not Found exception in cron job

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]import java.io.*;

import java.sql.Connection;

import java.sql.DriverManager;

class TestClassLoader

{

publicstaticvoid main(String[] args)

{

try{

Class.forName("com.mysql.jdbc.Driver");

String url ="jdbc:mysql://localhost/smsserver";

Connection con = DriverManager.getConnection(url,"username","password");

con.close();

}

catch (Exception ex){

ex.printStackTrace();

}

}

}

[3] server

/usr/bin:/bin

/home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar:.:/usr/local/jdk/lib/classes.zip:

/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:/usr/bin:/bin

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:164)

at TestClassLoader.main(TestClassLoader.java:10)

[4] server@imecserver1.com [~/public_html]# ll /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar

-rwxrwxrwx1 serverserver235712 Aug 7 13:17 /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar*

[3502 byte] By [Hughbedoa] at [2007-10-3 2:23:51]
# 1

how did u start the cron job?

check if the CLASSPATH setting is available to the cron job

cron job would be started as some user.

setup the CLASSPATH in the .profile file for the user

and if u are using the Jdbc driver inside the webcontainer

then CLASSPATH setting would not help. copy the

jdbc driver file to the appropriate lib folder

PMJaina at 2007-7-14 19:22:51 > top of Java-index,Java Essentials,New To Java...
# 2

> how did u start the cron job?

I used the vps providers web form

> check if the CLASSPATH setting is available to the

> cron job

> cron job would be started as some user.

> setup the CLASSPATH in the .profile file for the

> user

I have put in lines in the script to check this. I set the classpath here too. I quote the relvent bits here;

#> 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:

This sets the CLASSPATH to the same as when I run the script from the command line.

> and if u are using the Jdbc driver inside the

> webcontainer

> then CLASSPATH setting would not help. copy the

> jdbc driver file to the appropriate lib folder

I have copied the .jar file into a folder owned by the server user;

server@imecserver1.com [~/public_html]# ll /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar

-rwxrwxrwx1 serverserver235712 Aug 7 13:17 /home/server/lib/mysql-connector-java-3.0.15-ga-bin.jar*

Thanks for the input.

Hughbedoa at 2007-7-14 19:22:51 > top of Java-index,Java Essentials,New To Java...
# 3
I solved the problem, I ran the app with;java -classpath $CLASSPATH TestClassLoaderI still do not understand how this can fix it, but it will do for me.
Hughbedoa at 2007-7-14 19:22:51 > top of Java-index,Java Essentials,New To Java...
# 4
you could also have solved it by remembering to add the commandexport PATH CLASSPATHbefore the java command, but after setting the variables.
masijade.a at 2007-7-14 19:22:51 > top of Java-index,Java Essentials,New To Java...