Run a batch file from Task Scheduler is not working with a java command

霸气de小男生 提交于 2019-12-19 03:12:30

问题


Run a batch file from Task Scheduler is not working with a java command inside the .bat file. If I run the .bat file manually its working good.

Here is the simple .bat file I'm trying to schedule

set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_24;
set CMD= "%JAVA_HOME%\bin\java" -version

echo %CMD%
%CMD%

回答1:


When you type batchfile.bat on the command line, you are telling cmd.exe to read the file and execute each line it finds in it. When you double-click on your batch file in explorer, it calls cmd.exe for you, after reading the file associations in the registry.

Task Manager is not so kind.

So for your task to work, schedule it like this (from memory, not on a Windows box right now) :

cmd /c "c:\full\path\to\your\batchfile.bat"

For extra robustness, you could make sure you batch file run from a known directory, like the one that it reside in, by adding this at the top:

pushd %~dp0
REM .... The original batch file goes here ....
popd

And finally you could disable CMD autorun entry by adding /d right after cmd like this:

cmd /d /c "c:\full\path\to\your\batchfile.bat"



回答2:


If ixe013's suggestion doesnt work go to

'Actions'
'Edit' the task
'Start in (optional):'  Put the path to the directory  where the script is

So for the last one if you have 'C:\Users\Desktop\script.py' just put in 'C:\Users\Desktop\' in the 'Start in (optional):' field




回答3:


What worked for me was running the task as "Users" ( computername\Users ). Once I did that, and "run with highest privileges" checked, it ran without a hitch.




回答4:


Giving the full path of java.exe in the batch file fixed it for me. In a notepad, I typed the following line:

"C:\Program Files\Java\jdk1.8.0_40\bin\java.exe" -jar "C:\Users\usernameXXXX\Documents\NetBeansProjects\JavaApplication5\dist\JavaApplication5.jar"

Save this as a app1.bat file (C:\temp\app1.bat)

In the Actions tab of the task scheduler, give the path to the batch file, i.e, C:\temp\app1.bat Also, be careful in the Conditions tab of task scheduler- make sure you uncheck "Start the task only if the computer is on AC power"




回答5:


All other ways did not work for me, I followed this guide: http://richardstk.com/2012/06/15/scheduled-task-to-run-a-batch-file/#comment-6873

In order to get the batch file to run, I had to set the "Program\script" box to contain just the name of the script (ie. script.bat) and set the the folder path of the script in the "Start in (optional)" box




回答6:


I gave full permission to user Everyone from security tab from Properties of the folder in which batch file is. and it started working.




回答7:


What a coworker discovered on something he had that wasn't working, and I have verified on the system I had that wasn't working is the following:

When the whole task is initially setup, you HAVE TO initially use the radio button "Run only when user is logged on". It will ask for your password for the change.

Now run the task.

Verify that whatever the batch was supposed to do, did happen.

And THEN change to the radio button BACK TO 'Run whether user is logged on or not."

This solved a problem for both of us that we had individually been working on for hours.

Side notes: both issues were also trying to elicit a 3rd party FTP app (WinSCP and WinFTP respectively) in each of our cases. Regular "inhouse" batch/tasks were having no issues.



来源:https://stackoverflow.com/questions/19304652/run-a-batch-file-from-task-scheduler-is-not-working-with-a-java-command

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