简体   繁体   English

使用 cron 作业从特定 jar 运行 java 类

[英]Run java class from specific jar using cron job

I need to run a java class from a specific jar file using cron job in linux.我需要在 linux 中使用 cron 作业从特定的 jar 文件运行 java 类。 I have tried absolute paths for both java and the folder which contains all the jar files in a shell file and it runs manually just fine.我已经尝试了 java 和包含 shell 文件中所有 jar 文件的文件夹的绝对路径,并且它手动运行得很好。

#!/bin/sh
/absolute/path/to/java -cp "absolute/path/to/libs/*" com.xxx.MyClass

when I try to run this shell file from a cron job it fails当我尝试从 cron 作业运行这个 shell 文件时它失败了

* * * * * /bin/sh /absolute/path/to/script.sh

The folder libs contains multiple jar dependencies.文件夹 libs 包含多个 jar 依赖项。 I doubt that MyClass should be an absolute path also.我怀疑 MyClass 也应该是绝对路径。

That's NOT an 'absolute/path/to/libs'... that's a relative path.不是“绝对/路径/to/libs”……那是相对路径。

Additionally, you need to specify the jar file that contains your class此外,您需要指定包含您的类的 jar 文件

java -jar /absolute/path/to/jarfile.jar -cp /absolute/plath/to/libs com.xxx.MyClass 

Get yourself a decent tutorial on executing JAR files...给自己一个关于执行 JAR 文件的不错的教程......

https://www.baeldung.com/java-run-jar-with-arguments https://www.baeldung.com/java-run-jar-with-arguments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM