如何在cron下运行Java程序并导入jar [英] How to run a Java program under cron and import the jars
问题描述
我的源文件是 ... / MyDir / proj / myProj.java
。 jar文件在 ... / MyDir / proj / library
下。这些jar文件来自 HTMLUnit 2.10 。
My source file is .../MyDir/proj/myProj.java
. The jar files are under .../MyDir/proj/library
. The jar files are from HTMLUnit 2.10.
这是我的cron文件的来源:
This is the source for my cron file:
0 0 * * * java -classpath .../MyDir/proj/ myProj
但它给了我错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/gargoylesoftware/htmlunit/WebClient
如何修改cron文件以导入jar文件?
How do I modify the cron file to import the jar files?
推荐答案
像这样:
0 0 * * * java -classpath .../MyDir/proj/:.../MyDir/proj/library/jar1.jar:.../MyDir/proj/library/jar2.jar myProj
或者如果您使用的是最近的JVM,则可以使用通配符匹配所有JAR文件。
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
b $ b
(反斜杠很可能是不必要的,因为globbing不太可能匹配上下文中的任何内容...)
(The backslash is probably unnecessary because 'globbing' is unlikely to match anything in that context ...)
更好的是,将命令(和任何其他需要运行以准备启动)放入shell脚本,然后从crontab条目运行脚本。
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.
这篇关于如何在cron下运行Java程序并导入jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!