Reputation: 147
Issue Environment : Windows Server 2008 R2 Build 7601
I have a batch file SomeBatchFile.bat
which uses %~dp0
to get the script location. But when it's executed on Windows Server 2008 R2 Build 7601
from a java program, its showing a buggy behavior.
Here is the behavior,
1) When executed like this
Process proc= Runtime.getRuntime().exec("c:\\full\\path\\SomeBatchFile.bat");
Keeping the SomeBatchFile.bat
file in C:\full\path
(essentially giving the actual full path), its returning the expected result c:\full\path\
2) But when executed like this
Process proc= Runtime.getRuntime().exec("SomeBatchFile.bat");
Keeping the SomeBatchFile.bat
file in C:\Windows
(essentially a location that is a part of environment variable PATH
).This returns a wrong value instead of the BAT script location
, its returning the java program location
from where this script is called.
This is the script file I am using,
REM Just prints the script location to a file
set MY_HOME=%~dp0
echo %MY_HOME% >> test_out.txt
REM And some other business logic here ...
On Windows Server 2003, this is working absolutely fine.
Any idea why this happens like this ? Is this Java/Windows Bug ? And how to resolve this ?
Upvotes: 0
Views: 386
Reputation: 70943
I've tried to replicate your problem, and the only way I get to do it is if the batch file is executed quoted, that is, if when the java program invokes the cmd
instance to handle the batch file execution, the batch file name is "SomeBatchFile.bat"
. You can try to execute your batch file from command line calling it with quotes to see the problem.
That is, a simple batch file as
@echo %~dp0
placed somewhere in the path (by example, c:\windows\test.bat
), when called from command line, from any folder, without including the path to it, without quotes (just type test.bat
), you will retrieve the correct folder. But, if the same test is done calling the file as "test.bat"
you will retrieve the current folder
D:\>type c:\windows\test.bat
@echo %~dp0
D:\>
D:\>test.bat
C:\Windows\
D:\>"test.bat"
D:\
D:\>cd temp
D:\temp>test.bat
C:\Windows\
D:\temp>"test.bat"
D:\temp\
I don't have access to neither 2003 not 2008 to check, but "probably" the configuration on batch file invocation was changed.
Here (in this case the problem raised from c# and the reference is to the full batch file name, but it is the same problem) you will find why this happens.
If the batch file name does not contain spaces or special characters you can simply use something like
Runtime.getRuntime().exec("cmd.exe /c SomeBatchFile.bat")
Or, if you can not avoid the quotes in the file name, you can change the batch file to use a subroutine to obtain the required information
@echo off
setlocal enableextensions disabledelayedexpansion
call :getBatchFolder MY_HOME
echo %MY_HOME%
goto :eof
:getBatchFolder returnVar
set "%~1=%~dp0"
goto :eof
Upvotes: 2
Reputation: 2710
If you are trying to launch script in a relative path, you should also give a try for these syntaxes:
Process proc= Runtime.getRuntime().exec(".\\SomeBatchFile.bat");
or
Process proc= Runtime.getRuntime().exec(".\SomeBatchFile.bat");
or
Process proc= Runtime.getRuntime().exec("./SomeBatchFile.bat");
One of the three should work.
Upvotes: 2