6

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%
3
  • Chances are it is running, you just don't see the output. Under what user are you running the task under ? +1 For providing a SSCCE.
    – ixe013
    Commented Oct 10, 2013 at 19:40
  • Thanks for your response. User I'm trying to run has Admin access. Not sure if I'm scheduling it correctly using Task Scheduler. I have set up the action as "Start a Program" and in the Program/Script section I have specified the full path to the above .bat file Commented Oct 10, 2013 at 19:44
  • I have other simple java program scheduled and the java program generates log while its running, but I don't see the log at all. Commented Oct 10, 2013 at 19:46

8 Answers 8

11

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"
3
  • This still did not fix the problem for me - what seemed to work was Running As SYSTEM user (Run with Highest Privileges set). (I also tried running as NETWORK SERVICE but that didn't work) Commented Jan 31, 2014 at 11:06
  • Interresting, @Jamesmccormack. What error did you get ? What was the command line you used ? Did the user you selected had the rights or the environment setup to run java.exe ? It is never to late to provide a better answer to a question.
    – ixe013
    Commented Jan 31, 2014 at 15:46
  • 2
    This worked, however I should also add the user may need to add the correct directory to the Start In field depending on how the batch file was written.
    – techdude
    Commented Aug 25, 2014 at 18:22
8

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.

1

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"

0

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

0

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

0

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.

0

I had the same problem, and to solve it, I put the next command line into the batch file:

cd "CURRENT_DIRECTORY"

where CURRENT_DIRECTORY is the directory where the batch file is located.

Example: Suppose i have my batch file named test.bat located into c:\windows\system32\mytest

in my test.bat file, i introduce the next command line:

cd c:\windows\system32\mytest

Not the answer you're looking for? Browse other questions tagged or ask your own question.