windows 从任务计划程序运行批处理文件不适用于 java 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19304652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a batch file from Task Scheduler is not working with a java command
提问by user2371505
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.
从任务计划程序运行批处理文件不适用于 .bat 文件中的 java 命令。如果我手动运行 .bat 文件,它运行良好。
Here is the simple .bat file I'm trying to schedule
这是我正在尝试安排的简单 .bat 文件
set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_24;
set CMD= "%JAVA_HOME%\bin\java" -version
echo %CMD%
%CMD%
回答by ixe013
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.
当您batchfile.bat
在命令行上键入时,您是在告诉cmd.exe
读取文件并执行它在其中找到的每一行。当您在资源管理器中双击批处理文件时,它cmd.exe
会在读取注册表中的文件关联后呼叫您。
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) :
因此,为了让您的任务正常工作,请按如下方式安排(根据记忆,而不是现在在 Windows 机器上):
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
之后cmd
是这样的:
cmd /d /c "c:\full\path\to\your\batchfile.bat"
回答by Leon
If ixe013's suggestion doesnt work go to
如果 ixe013 的建议不起作用,请转到
'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
所以对于最后一个,如果你有 'C:\Users\Desktop\script.py' 只需在 'Start in (optional):' 字段中输入 'C:\Users\Desktop\'
回答by user3866475
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.
对我有用的是以“用户”(计算机名\用户)身份运行任务。一旦我这样做了,并选中了“以最高权限运行”,它就顺利运行了。
回答by user3739168
Giving the full path of java.exe in the batch file fixed it for me. In a notepad, I typed the following line:
在批处理文件中给出 java.exe 的完整路径为我修复了它。在记事本中,我输入了以下行:
"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)
将其另存为 app1.bat 文件 (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"
在任务计划程序的“操作”选项卡中,提供批处理文件的路径,即 C:\temp\app1.bat 另外,在任务计划程序的“条件”选项卡中要小心 - 确保取消选中“仅在以下情况下启动任务”计算机使用交流电源”
回答by Armand G.
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
所有其他方式对我都不起作用,我遵循了本指南: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
为了让批处理文件运行,我必须将“Program\script”框设置为只包含脚本的名称(即 script.bat),并在“Start in”中设置脚本的文件夹路径。 (可选)”框
回答by sansoftinfotech.co.in
I gave full permission to user Everyone from security tab from Properties of the folder in which batch file is. and it started working.
我从批处理文件所在文件夹的“属性”的“安全”选项卡中向用户“所有人”授予了完全权限。它开始工作了。
回答by Roger Ruffner
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.
旁注:在我们的每个案例中,这两个问题也都试图引出一个 3rd 方 FTP 应用程序(分别是 WinSCP 和 WinFTP)。常规的“内部”批次/任务没有问题。