0

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 ?

k0der
  • 137
  • 1
  • 10
  • Pass the correct path as the first parameter? – wOxxOm Sep 28 '15 at 11:15
  • The location of the file is not known in advance, the users keeps the script in any location which is a part of the PATH variable and executes the java program. This script picks the location of the script and do some activities. So I wanted to get the location of the script using %~dp0, which is failing on Windows 2008 Server. – k0der Sep 28 '15 at 11:22
  • Could you include the batch code that is retrieving the path? – MC ND Sep 28 '15 at 11:28
  • @MC ND - Script file content has been added to the question. – k0der Sep 28 '15 at 14:13
  • Silly question, but I have to ask: are you sure you don't have a copy of `SomeBatchFile.bat` in the folder containing the java program location? – MC ND Sep 28 '15 at 14:59
  • @MC ND - Yes, Java program and script are in different locations. – k0der Sep 28 '15 at 15:09
  • @MC ND - Basically the script is kept in a location which is a part of PATH environment variable. – k0der Sep 28 '15 at 15:34
  • @k0der, yes it was clear, thank you. I made the question because if the batch file is present in the *"current active folder"*, as it is implicitly included in the path, you end with a call to the wrong batch file. Before trying to search for bugs it is better to discard usual problems. – MC ND Sep 28 '15 at 16:08
  • Does %dp0 contain a space character? – lit Sep 29 '15 at 00:48

2 Answers2

2

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.

Paul
  • 2,620
  • 2
  • 17
  • 27
2

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
Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126