如何在 cron 下运行 Java 程序并导入 jars [英] How to run a Java program under cron and import the jars

查看:26
本文介绍了如何在 cron 下运行 Java 程序并导入 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

(反斜杠可能是不必要的,因为 '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 程序并导入 jars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆