Wednesday, November 26, 2014

Better checking your Java version

A couple of days ago I received a task to check installed applications in a bunch or servers. One of the things to check was the Java VM installed into those and see which versions was there.

It looks a simple task, but it is not: some applications requires different versions of Java, so there is none "one JVM to rule them all".

It should a be simple thing to do it automatically, but actually Java doesn't help as much I as would like. This is the output from Java in my MS Windows box:

C:\>java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) Client VM (build 24.71-b01, mixed mode, sharing)

C:\>


The version is there, right? Well, it is, but can you tell me if this JVM is 64 or 32 bits version?

The truth is, if the JVM is 64 bits it should let me know... but for 32 I'm just guessing. That's OK when I'm doing it manually, but guessing from script is not so.

After some searching over the Internet I got this ridiculous simple code, actually I could say this could be the "Hello World" example of Java, and here it goes:

public class JavaArch {
 

    public static void main(String[] args) {
                          System.out.println(System.getProperty("sun.arch.data.model"));


    }

}


Compile that code with javac and ship it with your script and you're good to go. No guessing required, just the bare information.

C:\>javac JavaArch.java

C:\>java JavaArch
32

C:\>


Can't be easier than that.

See more information about here.

If you need even more properties available for the environment from the JVM point of view, go checking http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties%28%29.

Here is an example from my environment:

C:\>java JavaArch
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = C:\Program Files (x86)\Java\jre7\bin
java.vm.version = 24.71-b01
user.country.format = BR
java.vm.vendor = Oracle Corporation
java.vendor.url = http://java.oracle.com/
path.separator = ;
java.vm.name = Java HotSpot(TM) Client VM
file.encoding.pkg = sun.io
user.script =
user.country = US
sun.java.launcher = SUN_STANDARD
sun.os.patch.level = Service Pack 1
java.vm.specification.name = Java Virtual Machine Specification
user.dir = C:\Users\foobar\Documents\temp
java.runtime.version = 1.7.0_71-b14
java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs = C:\Program Files (x86)\Java\jre7\lib\endorsed
os.arch = x86
java.io.tmpdir = C:\Users\foobar\AppData\Local\Temp\
line.separator =

java.vm.specification.vendor = Oracle Corporation
user.variant =
os.name = Windows 7
sun.jnu.encoding = Cp1252
java.library.path = C:\Program Files (x86)\Java\jre7\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\oracle\product\11.2.0\client_1\bin;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Lenov
o\Fingerprint Manager Pro\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;C:\oracle\product\11.2.0\client_1\bin;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Lenovo\Fingerprint
Manager Pro\;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin;C:\Program Files (x86)\GnuWin32\bin;C:\Program Files (x86)\Java\jre7\bin;C:\Program Files (x86)\PuTTY;C:\Users\foobar\Downloads\sqldeveloper\jdk\bin;.
java.specification.name = Java Platform API Specification
java.class.version = 51.0
sun.management.compiler = HotSpot Client Compiler
os.version = 6.1
user.home = C:\Users\foobar
user.timezone =
java.awt.printerjob = sun.awt.windows.WPrinterJob
file.encoding = Cp1252
java.specification.version = 1.7
user.name = foobar
java.class.path = .
java.vm.specification.version = 1.7
sun.arch.data.model = 32
java.home = C:\Program Files (x86)\Java\jre7
sun.java.command = JavaArch
java.specification.vendor = Oracle Corporation
user.language = en
user.language.format = pt
awt.toolkit = sun.awt.windows.WToolkit
java.vm.info = mixed mode, sharing
java.version = 1.7.0_71
java.ext.dirs = C:\Program Files (x86)\Java\jre7\lib\ext;C:\windows\Sun\Java\lib\ext
sun.boot.class.path = C:\Program Files (x86)\Java\jre7\lib\resources.jar;C:\Program Files (x86)\Java\jre7\lib\rt.jar;C:\Program Files (x86)\Java\jre7\lib\sunrsasign.jar;C:\Program Files (x86)\Java\jre7\lib\jsse.jar;C:\Program Files (x86)\Java\jre7\lib\jce.jar;C:\Program Files (x86)\Java\jre7\lib\charsets.jar;C:\Program Files (x86)\Java\jre7\lib\jfr.jar;C:\Program Files (x86)\Java\jre7\classes
java.vendor = Oracle Corporation
file.separator = \
java.vendor.url.bug = http://bugreport.sun.com/bugreport/
sun.cpu.endian = little
sun.io.unicode.encoding = UnicodeLittle
sun.desktop = windows
sun.cpu.isalist = pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86


And the updated code to retrieve that:

import java.util.*;

public class JavaArch {

    public static void main(String[] args) {
   
        Properties props = System.getProperties();
        Enumeration names = props.propertyNames();
       
        while (names.hasMoreElements()) {
       
            String current = names.nextElement().toString();
       
            System.out.println(current + " = " + System.getProperty(current));
       
        }
   
    }

}

No comments:

Post a Comment