Windows Bat: Detect JVM is 32 or 64 bit


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.
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" 
 
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

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)