Find location of java.exe
To run a java application, we need to find the java path: we can use define our own environment variable like MYAPP__JAVA_HOME or use JAVA_HOME or JRE_HOME or we can use system default java.
The code looks like below:
Detect JVM is 32 or 64 bit
Afterwards, we may have to detect JVM is 32 or 64 bit, so we can set different jvm parameters according: for example, if it's 32 bit java, set -Xmx 2g; if it's 64 bit java, set -Xmx 4g.
To detect 32 or 64 bit java, the code looks like below:
set is64Java=false
java -version 2>&1 | findstr /i "64-Bit" 1>nul 2>&1
if %errorlevel% == 0 (
set is64Java=true
)
Plain old java -version reveals information about JVM bitness only if installed JRE is 64 bit, in case
of 32 bit JVM it doesn't provide any information related to architecture but in case of 64 bit JVM it
prints :
C:\>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
while in case of 32 bit JVM it will print
C:\> java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)
'java -version 2>&1 | findstr /i "64-Bit"' checks whether there is 64-Bit in java -version, if so, it means it's a 64 bit jdk, if not, it's a 32 bit jdk.
Resources
http://stackoverflow.com/questions/5675459/how-to-get-java-version-from-batch-script
http://javarevisited.blogspot.com/2012/01/find-jvm-is-32-or-64-bit-java-program.html
http://blogs.msdn.com/b/oldnewthing/archive/2012/07/31/10334556.aspx
The code looks like below:
Detect JVM is 32 or 64 bit
Afterwards, we may have to detect JVM is 32 or 64 bit, so we can set different jvm parameters according: for example, if it's 32 bit java, set -Xmx 2g; if it's 64 bit java, set -Xmx 4g.
if not "%MYAPP__JAVA_HOME%" == "" ( set "CURRENY_JAVA_HOME=%CV_JAVA_HOME%" ) else ( if not "%JAVA_HOME%" == "" ( set "CURRENY_JAVA_HOME=%JAVA_HOME%" ) else ( if not "%JRE_HOME%" == "" ( set "CURRENY_JAVA_HOME=%JRE_HOME%" ) } ) set _RUNJAVA="%MYAPP__JAVA_HOME%\bin\java"
set is64Java=false
java -version 2>&1 | findstr /i "64-Bit" 1>nul 2>&1
if %errorlevel% == 0 (
set is64Java=true
)
Plain old java -version reveals information about JVM bitness only if installed JRE is 64 bit, in case
of 32 bit JVM it doesn't provide any information related to architecture but in case of 64 bit JVM it
prints :
C:\>java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
while in case of 32 bit JVM it will print
C:\> java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)
'java -version 2>&1 | findstr /i "64-Bit"' checks whether there is 64-Bit in java -version, if so, it means it's a 64 bit jdk, if not, it's a 32 bit jdk.
Resources
http://stackoverflow.com/questions/5675459/how-to-get-java-version-from-batch-script
http://javarevisited.blogspot.com/2012/01/find-jvm-is-32-or-64-bit-java-program.html
http://blogs.msdn.com/b/oldnewthing/archive/2012/07/31/10334556.aspx