Search code examples
javacronterminalnoclassdeffounderror

How to run a Java program under cron and import the jars


My source file is .../MyDir/proj/myProj.java. The jar files are under .../MyDir/proj/library. The jar files are from HTMLUnit 2.10.

This is the source for my cron file:

0 0 * * * java -classpath .../MyDir/proj/ myProj

But it gave me the error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/WebClient

How do I modify the cron file to import the jar files?


Solution

  • Something like this:

    0 0 * * * java -classpath .../MyDir/proj/:.../MyDir/proj/library/jar1.jar:.../MyDir/proj/library/jar2.jar myProj
    

    or if you are using a recent JVM you can use a wildcard to match all of the JAR files.

    0 0 * * * java -classpath .../MyDir/proj/:.../MyDir/proj/library/\* myProj
    

    (The backslash is probably unnecessary because 'globbing' is unlikely to match anything in that context ...)


    Better still, put the command (and any others that need to be run to prepare for the launch) into a shell script, and run the script from your crontab entry instead.