2

So when i manually start my bat file that is on desktop, it works because executing a bat file via its icon uses directory of the icon(file) which is "C:\Users\Michael\Desktop".

set CLASSPATH = %~dp0 &:: %~dp0 stands for the directory of the bat file
java InitArray 5 0 4 &:: comment
pause

When i run the bat file above(InitArray.bat) from its desktop icon, it works.

java InitArray 5 0 4 &:: comment
pause

When i run the bat file above(test.bat) from its desktop icon, it works.

But when i run InitArray.bat from task scheduler, it uses the directory "C:\WINDOWS\system32". But that shouldnt be a problem because the first thing the bat file does is "set CLASSPATH = C:\Users\Michael\Desktop\".

Here is the result of task scheduler trying to run InitArray.bat.

C:\WINDOWS\system32>set CLASSPATH = C:\Users\Michael\Desktop\

C:\WINDOWS\system32>java InitArray 5 0 4
Error: Could not find or load main class InitArray

C:\WINDOWS\system32>pause
Press any key to continue . . .

Now, i know i can fix this issue by adding "C:\Users\Desktop\" to environment variable CLASSPATH. But i shouldnt need to do that since i am manually setting classpath to desktop in the first line of my bat file before trying to run the java class.

4
  • Your code is filled with illegal stuff, why would anyone read it? Remove ALL MALFORMED lable. They start with :: which tells us you copied this stuff. Commented Jan 29, 2019 at 9:34
  • 1
    set CLASSPATH = %~dp0 sets a variable called CLASSPATH + SPACE, so remove the spaces around the =-sign... Ah, and remove the &:: stuff (it is often fine, but it can cause terrible problems); for comments, use rem... Commented Jan 29, 2019 at 9:51
  • @catcat No i didnt copy this stuff. i am not an expert in dos programming. I thought &:: was just a harmless comment just like // in java. Commented Jan 29, 2019 at 10:10
  • 1
    Alternatively, cd %~dp0. Or java -cp "%~dp0" InitArray 5 0 4. Commented Jan 29, 2019 at 10:32

2 Answers 2

2

When setting a variable, everthing from the beginning of the variable name, until the last typed character is used as part of the variable name, before the = and value after the =. So:

set CLASSPATH = Somepath

Will end up with a variable name %CLASSPATH % (note the trailing space) and value Somepath (note the starting space.

Even if you add an accidental space after the value, it will become part of it, So this set CLASSPATH=Somepath will end up with value with a trailing space Somepath

So always leave no space before or after the = and always enclose your code in double quotes to eliminate whitespace:

set "CLASSPATH=Somepath"

or in your actual case, it should look like:

set "CLASSPATH=%~dp0"
Sign up to request clarification or add additional context in comments.

1 Comment

I guess i could use echo and pause to debug my bat programs (print values of variables). Anyway, thanks for the answer.
0

Set the CLASSPATH along with the java execution command or change directory using cd command.

cd /path_to_required_folder   // set current directory
java -cp /classpath_location/ test.jar your.package.MainClass [args...]

Hope this helps.

1 Comment

I didnt want to use cd command because i want the bat file to be dynamic(run no matter where the user moves the folder that includes the bat file and java class file).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.