Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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%
like image 986
user2371505 Avatar asked Oct 10 '13 19:10

user2371505


2 Answers

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"
like image 99
ixe013 Avatar answered Nov 06 '22 15:11

ixe013


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

like image 35
Leon Avatar answered Nov 06 '22 16:11

Leon