简体   繁体   English

使用任务调度程序运行java jar(来自批处理文件)

[英]Using task scheduler for running java jar (from batch file)

I am scheduling a task (with windows task scheduler) which simply run a batch file. 我正在安排一个任务(使用Windows任务调度程序),它只运行一个批处理文件。

this batch is running a jar file (Java program) with a simple "Java -jar test.jar". 这个批处理正在运行一个带有简单“Java -jar test.jar”的jar文件(Java程序)。

When i run the script from the command line manually, the java program runs well and no exception is shown. 当我从命令行手动运行脚本时,java程序运行良好,没有显示异常。

but when the task schedular does the same (calling the command), the java program ends with an exception: "ClassNotFoundException" for one of the classes which is in my jar. 但是当任务调度程序执行相同操作(调用命令)时,java程序以异常结束:“ClassNotFoundException”,用于我的jar中的一个类。

What way be the cause of this? 这是什么原因导致的? what is the diffrence when calling the jar from the command line and from the task scheduler? 从命令行和任务调度程序调用jar时的差异是什么?

Thanks. 谢谢。

I reckon that probably the "current directory" is different, and as a consequence java doesn't find the jar at all. 我估计可能“当前目录”不同,因此java根本找不到jar。 In the first line of your .bat, can you do a cd \\path\\that\\you\\expect before you execute java? 在你的.bat的第一行,你可以在执行java之前做一个cd \\path\\that\\you\\expectcd \\path\\that\\you\\expect吗?

Does your jar have any dependencies? 你的jar有任何依赖吗? Also, it'd be helpful to know what's your folder structure, and how exactly do you run it in the command line. 此外,知道您的文件夹结构是什么以及如何在命令行中运行它是有帮助的。
Anyways, depending on your case, you can do something along these lines: 无论如何,根据你的情况,你可以做这些事情:

cd /path/to/exec/folder   // set current directory
java -cp /all-classpath-jars/and-or-bin-folders/ test.jar your.package.MainClass [args...]

This has to work, if you specify everything you need correctly. 如果您正确指定所需的一切,这必须起作用。

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

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