views:

97

answers:

1

I want to write a simple batch file where i want to setup a environment variable based on the machine architecture. It is as below:

set ARCH=%PROCESSOR_ARCHITECTURE%
echo %ARCH%
if %ARCH%==x86 (
  set JAVA_ROOT=C:\Progra~1\Java\j2re1.4.2_13
) else (
  set JAVA_ROOT=C:\Progra~2\Java\j2re1.4.2_13
)
echo JAVA_ROOT is %JAVA_ROOT%

On 64-bit machine where the architecture is 'AMD64' the JAVA_ROOT will be displayed as 'C:\Progra~2\Java\j2re1.4.2_13' at the echo statement. But when i run an application that uses this file, the first value of JAVA_ROOT would be picked up 'C:\Progra~1\Java\j2re1.4.2_13'. I don't have any idea why it goes in the 'if' part even though i am running this on 64-bit Windows7. When i echoed the

A: 

If you're running the batch file using %SystemRoot%\syswow64\cmd.exe on 64-bit Windows, perhaps because you're starting it from a 32-bit app, then %PROCESSOR_ARCHITECTURE% will be equal to x86, not AMD64. To detect this situation, you can use the %PROCESSOR_ARCHITEW6432% variable. Here's a blog post with more info.

However, if you just want to find the 32-bit Java path, you don't have to worry about that because WOW64 will take care of it for you if you use the %ProgramFiles% variable:

if "%PROCESSOR_ARCHITECTURE%" == "x86" set JAVA_ROOT=%ProgramFiles%\Java\j2re1.4.2_13
if "%PROCESSOR_ARCHITECTURE%" == "AMD64" set JAVA_ROOT=%ProgramFiles(x86)%\Java\j2re1.4.2_13
if not defined JAVA_ROOT (
  echo Unsupported processor architecture.
  exit /b 1
)
if not exist %JAVA_ROOT%\. (
  echo Java 1.4.2_13 is not installed.
  exit /b 1
)

Note that I avoided the if condition ( command ) else ( command ) form for setting JAVA_ROOT. This is because %ProgramFiles(x86)% contains parentheses, which would cause cmd.exe to misparse the if-statement if I used that form. For more complicated commands or more complicated conditions, using call to call a subroutine might be better. (Using a more expressive language would be even better, but that doesn't answer the question.)

bk1e
I tried to get value of %PROCESSOR_ARCHITEW6432% by doing 'echo %PROCESSOR_ARCHITEW6432%', but i get 'ECHO is off.' Echo is not off as i do get other echo values. Is it because it is undefined? Can you please show me how to do comparison in batch syntax?
Sam
Yes, it's not defined except for WOW64, as stated by the article I linked to. Are you running your batch file from another program or from a command prompt window? Anyway, it just occurred to me that if you just use %ProgramFiles% and %ProgramFiles(x86)% instead of hardcoding paths, you won't have to worry about the WOW64 case.
bk1e
I am running the batch file from another program. When i run the batch file from command prompt it works fine. But when i run it from another program, it gets architecture as x86. Your other approach doesn't work from me, because of the spaces in the Program files path.I guess if I can find out the syntax to do the comparison and just keep the hard coded paths, my job would be done. I tried the synatx you provided above with the hardcoded paths, and it still goes inside x86 when called through another program
Sam
Did you try adding quotes around the variable when you evaluate it? I.e. `"%JAVA_ROOT%"`
bk1e