Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Class Not Found exception in cron job  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Huw Morgan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1154
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1154
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!