Anush M
Anush M

Reputation: 25

Windows batch script unable to find the path for library through windows scheduler

I have a java program which connects to my database and verify some reports and send out an email to the specific mail ids. And there is one batch script which calls this java class. This java class requires javax.activation.jar, javax_mail.jar, ojdbc14.jar which I Kept along with batch and java file in some folder.

When I run the program by executing the batch script directly from the path where it lies the program works and send out emails.

But now I have scheduled this batch script to run every one hour using the windows scheduler. At that point there is compilation error for my java saying unable to find the required dependent classes.

My batch file looks like this.

@echo off

cd D:\CPP\scheduler\JOB_REPORTS_MAIL
D:

:: compile the Main class and helper classes
javac Constants.java
javac DBUtility.java
javac -cp ".;javax.activation.jar;javax_mail.jar;ojdbc14.jar" SendJobStatusEmail.java
javac -cp ".;javax.activation.jar;javax_mail.jar;ojdbc14.jar" JobReportScheduler.java


:: Run the Main class
java -cp ".;javax.activation.jar;javax_mail.jar;ojdbc14.jar" JobReportScheduler

If I remove the below lines from my batch its not able to run through the windows scheduler

cd D:\CPP\scheduler\JOB_REPORTS_MAIL
D:

I want to remove these two lines. WHen the scheduler picks the batch file and executes it it should be able to pick the dpendent jars from lib folder under the directory where the batch stays.

It should not be dependent on the machine where, in which drive it it so that I can put in any windows machine and execute using windows scheduler.

I tried different options nothing works. PLease help me here.

Upvotes: 0

Views: 767

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200443

You could Replace . in your CLASSPATH with D:\CPP\scheduler\JOB_REPORTS_MAIL if you just ran the compiled code. However, you compile the code each time the script runs, and these lines:

javac Constants.java
javac DBUtility.java

probably depend upon the current working directory being D:\CPP\scheduler\JOB_REPORTS_MAIL, so I don't think you'll be able to get around changing into that directory (unless you modify your build process, e.g. by using ant).

Why do you want to avoid changing the directory anyway? If you're concerned about the working directory being different after the script runs, you could use pushd and popd instead of cd:

@echo off

pushd "D:\CPP\scheduler\JOB_REPORTS_MAIL"
javac ...
...
java -cp ...
popd

but that doesn't really matter for a scheduled task.

And why do you want a scheduled task to re-compile your code over and over again in the first place?


Actual solution: Since the javac calls aren't present in the actual script, the code can be simplified to this:

@echo off

java -cp ".;.\lib\*" JobReportScheduler

when D:\CPP\scheduler\JOB_REPORTS_MAIL is configured as the start directory of the scheduled task.

Upvotes: 0

Endoro
Endoro

Reputation: 37589

Try this:

@echo off &SETLOCAL 
SET "Jpath=%cd%"

:: compile the Main class and helper classes
javac Constants.java
javac DBUtility.java
javac -cp "%Jpath%;javax.activation.jar;javax_mail.jar;ojdbc14.jar" SendJobStatusEmail.java
javac -cp "%Jpath%;javax.activation.jar;javax_mail.jar;ojdbc14.jar" JobReportScheduler.java


:: Run the Main class
java -cp "%Jpath%;javax.activation.jar;javax_mail.jar;ojdbc14.jar" JobReportScheduler

Upvotes: 1

Related Questions