3

I wrote a simple .BAT script, I scheduled it in windows server for backup purposes. The problem is that it runs an application, but the output doesn't go in the folder it is in, it goes somewhere (I don't know where). The output is a log file and a backup file.

Here's the script:

"%JAVA_HOME%"\bin\java -jar "%~dp0jwbackup.jar"
gotch4
  • 13,093
  • 29
  • 107
  • 170

3 Answers3

7

You have two choices. The first is that you can set the current directory for the scheduled task the same way you can specify the executable. This is, of course, an extra thing you need to worry about if the location ever changes.

The other way is shown in your script already. The %~dp0 in your jarfile specification is the drive and path of argument zero (the batch file name) so it looks for the jarfile in the same directory as your batch file.

Hence you can just put:

cd /d %~dp0

into your command file before attempting to run your Java program and it will be in the correct directory, even if you decide to move it to somewhere else.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

You can just cd into the folder before running the actual application:

cd\
cd "%JAVA_HOME%"\bin
java -jar "%~dp0jwbackup.jar"
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • You probably don't want to be in the Java bin directory, rather I think the requirement is to be in the same directory as the batch file. But the theory is correct, you just have to change the `cd` command to select the different location. – paxdiablo Nov 21 '11 at 12:20
  • I don't know anything about Java, so I can't tell whether putting your output file in the `bin` directory is a good idea. He doesn't clearly state in his question if he wants the output in the batch file directory or in the `bin` directory (I understood the latter). If he wants the batch file directory, then your answer ist the better one. – Christian Specht Nov 21 '11 at 12:22
1

Add cd /d %~dp0 to set the currect working directory to the location of the .bat file.

Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124