Developers, Developers, Developers! Maksim Sorokin IT Blog

28Jul/11Off

Discovering Version of Java in a BAT File

Sometimes in a BAT file we need to know what Java version are we running -- is it 32bit or 64bit. Here is a sample bat file, which depending on a Java version uses different native library directory:

@echo off

:: At this point you can place assumptions about Java version. It is still better than nothing anyway. In this particular case, we rely on a knowledge of Java version during installation time (with IzPack). Or you can just specify default value here, for example by default assume that 32 version is being run
set JVM_VERSION=""
if $SYSTEM_os_arch==x86 (
  set JVM_VERSION=32
) else (
  set JVM_VERSION=64
)
:: End of assumption section

:: Now trying to find out, what is current jvm version.

set TEMP_FILE=%TEMP%\javaCheck%RANDOM%%TIME:~9,5%.txt

java -version 2>%TEMP_FILE%
FOR /F "tokens=*" %%i in (%TEMP_FILE%) do (
  echo %%i | find "HotSpot" >nul
  if not errorlevel 1 (
    echo %%i | find "64-Bit" >nul
    if not errorlevel 1 (
      set JVM_VERSION=64
    ) else (
      set JVM_VERSION=32
    )
  )
)

del %TEMP_FILE%

if %JVM_VERSION%==32 (
  set NATIVE_JARS=$INSTALL_PATH/lib/32
) else (
  set NATIVE_JARS=$INSTALL_PATH/lib/64
)

start "App" javaw -classpath .;"%NATIVE_JARS%/*" my.app.App

The trick is to redirect java -version to a temporary file. Then try to find a line with "HotSpot" substring. If that line contains "64-Bit", then it is 64bit version Java. Otherwise it is 32bit.

24Mar/11Off

IzPack. Adding 64bit support.

IzPack should work everywhere. However, you have to be aware about several things if you have support for 64bit machines.

First, if you are using COI tools from IzPack (for example if you have your own custom panel that works with registry or using CheckedHelloPanel) you also have to deliver 64bit dll:

  <native type="3rdparty" name="COIOSHelper.dll" stage="both">
    <os family="windows" />
  </native>
  <native type="3rdparty" name="COIOSHelper_x64.dll" stage="both">
    <os family="windows" />
  </native>

The second thing, if you are using ShortcutPanel, you will also have to deliver 64bit dlls:

  <native type="izpack" name="ShellLink.dll">
    <os family="windows" />
  </native>
  <native type="izpack" name="ShellLink_x64.dll">
    <os family="windows" />
  </native>