Confusing issue with batch as a Scheduled Task
@ECHO ON
SET COMMON_LIB=commons-io-1.3.1.jar;
SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB%
java -cp %AR_CLASS_PATH% -Xms128m -Xmx256m FileCreating
PAUSE
When I run the batch file directly, i.e. double cliking on the .bat file, it runs fine, the command window opens up and ex开发者_JAVA技巧ecutes all the required commands( note the PAUSE
).
Running
) but I cant see the command window and so I cant make out if it has been processed or the error it has generated.
And so, cant understand if the error is in classpath or my java code or somewhere else.
The environment is Windows Server 2003 R2 EE, SP2. The user has Admin priviledges.
I checked but there is no file of Schedlgu.txt inWINDOWS\Tasks
dir.
One thing I noticed was the CLASSPATH
value had no reference to the jdk/bin, can that be a issue?
Please advise.
EDIT
Just to simplify things, I commented the java
command for the bat file to do almost nothing then set some variables and then pause to keep the window open. Still no success.
Scheduled tasks do not necessarily open a visible window.
In my experience, if there is an active desktop session for the relevant user at the time the scheduled tasks starts, then it will usually create a window in that session. If there is more than one such session, it will select one of them, but I am not sure how the choice is made. If there is no such active session, then no window will be created at all.
It seems most likely that the job is running through and then stopping at the PAUSE command, with no way to receive input that would make it continue. This is difficult to verify since PAUSE is an internal command and will not show up as a separate process in Task Manager or Process Explorer. But if you can verify that there is no java process running, this would support the belief that the java portion of the batch has completed and it is stuck on the PAUSE.
In general, when running a batch file as a scheduled task, it is wise to redirect both standard output and standard error to a file so you can look for errors after the job has completed.
Well, after some trial and error, I was finally able to get my scheduled task to run.
As suggested by Dave, I had the std out redirected to a text file, so that I can see what errors are generated. But the text file came out blank and the task was run.
So I added a java -version
@ECHO ON
SET COMMON_LIB=commons-io-1.3.1.jar;
SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB%
java -version
java -cp %AR_CLASS_PATH% -Xms128m -Xmx256m FileCreating
and still no result, even now the text file was blank.
Then, I created a simple HelloWorld
program and added this java command to the bat file
@ECHO ON
SET COMMON_LIB=commons-io-1.3.1.jar;
SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB%
java -version
java HelloWorld > C:\ChechHW.txt
java -cp %AR_CLASS_PATH% -Xms128m -Xmx256m FileCreating
The CheckHW.txt had the output text, I had given in the HelloWorld
program.
So now all these added to the confusion and annoyance.
Interestingly and surprisingly, the issue was the CLASSPATH
variable I am using to set the classpath with the java
command.
I dont know how and importantly why it started working after I removed the %CLASSPATH%
from
SET AR_CLASS_PATH=%CLASSPATH%%COMMON_LIB%
The bat file in my question was and is working on a Windows XP system without this change.
I wonder some setting on the Windows 2003 Server related to Java class path was not allowing it to process.
精彩评论