Shell文件在手动运行时有效,但在CRON中运行时失败

时间:2011-08-17 18:40:53

标签: java cron classpath aix

我有一个shell文件(compile.sh),编译然后运行一个java程序。它在我在AIX服务器上手动运行时工作,但是当我通过CRON运行它时给我一个错误。

compile.sh:

/usr/java6/bin/javac  -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/usr/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar -d . daily_transmission.java

java -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/urs/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar daily_transmission

错误:

/ttms/its/db/dropez/trans/compile_java.sh
javac: file not found: *.java
Usage: javac <options> <source files>
use -help for a list of possible options
Exception in thread "main" java.lang.NoClassDefFoundError: daily_transmission
Caused by: java.lang.ClassNotFoundException: daily_transmission
at java.net.URLClassLoader.findClass(URLClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:653)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:346)
at java.lang.ClassLoader.loadClass(ClassLoader.java:619)
Could not find the main class: daily_transmission. Program will exit. 

1 个答案:

答案 0 :(得分:4)

在编译和运行之前只需更改目录:

cd /absolute/path/to/your/project

/usr/java6/bin/javac  -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/usr/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar -d . daily_transmission.java

java -classpath :.:/usr/jdk/commons-net-3.0.1.jar:/usr/jdk/classes12.jar:/usr/jdk/mysql-connector-java-5.1.17-bin.jar:/usr/jdk/jtds-1.2.5.jar:/urs/jdk/mail.jar:/usr/jdk/joda-time-1.6.2.jar:/usr/jdk/commons-codec-1.4.jar:/usr/jdk/commons-logging-1.1.1.jar:/usr/jdk/httpclient-4.1.1.jar:/usr/jdk/httpclient-cache-4.1.1.jar:/usr/jdk/httpcore-4.1.jar:/usr/jdk/httpmime-4.1.1.jar:/usr/jdk/mailapi.jar:/usr/jdk/pop3.jar:/usr/jdk/smtp.jar:/usr/jdk/dsn.jar:/usr/jdk/imap.jar daily_transmission

** 更新 **

实际上,cron执行用户主目录中的每个作业。因此,最好从shell脚本更改工作目录。甚至可以通过指定每个文件和文件夹的绝对路径来编写您的查询,就像您坐在主目录(或根目录)中一样。