2


I have a DOS batch file to run on a daily basis.
Something similar like -

@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 executes all the required commands( note the PAUSE ).
But when I schedule a daily task, I see the status as Running. Also, when I right click on the task, it gives me an option to end the task(when status is 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 in WINDOWS\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.

Swift-Tuttle
  • 485
  • 3
  • 14
  • 25
  • I don't understand what your problem is. Is your task failing? Is there an error somewhere? – Gabe Jan 06 '11 at 13:46
  • Same here, even I dont understand if the task is failing or not. Thats the problem I dont see an error anywhere which makes it a lot more difficult. – Swift-Tuttle Jan 06 '11 at 13:53

2 Answers2

0

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.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
  • Thanks for the explanation, I am still not fully convinced though. I tried the same thing on Windows XP and it was absolutely fine. The scheduled task did open a console and paused at the PAUSE command. You have a nice point though to ponder upon. I will also try with the redirecting the stdout. – Swift-Tuttle Jan 06 '11 at 13:48
  • Thanks a lot Dave for your suggestion. – Swift-Tuttle Jan 07 '11 at 14:25
  • I would add that rather than using PAUSE, you could try running the Java command with: `start /WAIT java ...` - this in theory should cause the command shell to wait until the java program has exited. You should then not be left with a "dangling" process when running it as a scheduled task. – icabod Jan 07 '11 at 14:52
  • @icabod, Well my main issue was not about 'dangling' process caused by PAUSE, as even without the PAUSE nothing was happening. As I later found out that for some reason the java command was not getting executed and eventually found that it was due to setting of classpath in the java command. I have explained in my answer how I overcame this problem. Though I still dont understand why with the `CLASSPATH` variable it was not running. – Swift-Tuttle Jan 07 '11 at 23:25
0

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.

Swift-Tuttle
  • 485
  • 3
  • 14
  • 25