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

拜拜、爱过 提交于 2019-12-13 15:05:09

问题


I am scheduling a task (with windows task scheduler) which simply run a batch file.

this batch is running a jar file (Java program) with a simple "Java -jar test.jar".

When i run the script from the command line manually, the java program runs well and no exception is shown.

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.

What way be the cause of this? what is the diffrence when calling the jar from the command line and from the task scheduler?

Thanks.


回答1:


I reckon that probably the "current directory" is different, and as a consequence java doesn't find the jar at all. In the first line of your .bat, can you do a cd \path\that\you\expect before you execute java?




回答2:


Does your jar have any dependencies? 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.



来源:https://stackoverflow.com/questions/14100042/using-task-scheduler-for-running-java-jar-from-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!