简体   繁体   中英

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.

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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