2011-08-17 148 views
1

我有一個編譯的shell文件(compile.sh),然後運行一個java程序。當我在AIX服務器上手動運行它時會起作用,但是當我通過CRON運行它時會出現錯誤。Shell文件在手動運行時工作,但在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. 

回答

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腳本中改變你的工作目錄。甚至通過爲每個文件和文件夾指定絕對路徑來編寫查詢,就好像您坐在主目錄(或根目錄)中一樣。

相關問題