Link to home
Start Free TrialLog in
Avatar of snajalm
snajalm

asked on

Identifying the bit level of an operating system in Java!

How would we identify in java if a platform our application is running on is either 64bit or 32bit in side the code to choose the right driver for the application install??!
Avatar of for_yan
for_yan
Flag of United States of America image

go to cmd dos window

make

echo %PATH%

and copy paste here the output
also in command line tyepe

java -version


You may see:
      

on WindowsNT you just type java -version in your console

and if a 64bit version is running, you'll get a message like

java version "1.6.0_18" Java(TM) SE Runtime Environment (build 1.6.0_18-b07) Java HotSpot(TM) 64-Bit Server VM (build 16.0-b13, mixed mode)


see here:
http://stackoverflow.com/questions/2062020/how-can-i-tell-if-im-running-in-64-bit-jvm-or-32-bit-jvm

Avatar of snajalm
snajalm

ASKER

No!  Imagine whithin your code you would need to identify your operating system bit level automatically so that the software knows whether it's running a 64bit version or a 32bit version! I don't want to get it through the command prompt or in any manual form!
cehck in the link above - that what is suggested in the code:


Sun has a Java System property to determine the bitness of the JVM: 32 or 64:

sun.arch.data.model=32 // 32 bit JVM
sun.arch.data.model=64 // 64 bit JVM

You can use

System.getProperty("sun.arch.data.model")

to determine if its 32/64 from the program.

From the sun.docs:

    When writing Java code, how do I distinguish between 32 and 64-bit operation?

    There's no public API that allows you to distinguish between 32 and 64-bit operation. Think of 64-bit as just another platform in the write once, run anywhere tradition. However, if you'd like to write code which is platform specific (shame on you), the system property sun.arch.data.model has the value "32", "64", or "unknown".

The only good reason is if your java code is dependent upon native libraries and your code needs to determine which version (32 or 64bit) to load on startup.
Avatar of snajalm

ASKER

How would it be to identify the JRE installed at the code level??!
that is how you do it on the code level  - they explain it above:

if(System.getProperty("sun.arch.data.model").equals("64")) {
//it is 64-bit

} else
{
// it is 32
}

To find out the bitness of the platform u r running,try the following code ...

System.getProperty("os.arch"); // to get OS architecture

# on a 64-bit Linux box, I am getting x86_64..
Avatar of snajalm

ASKER

Great, NOW how do you check which JRE version is installed assuming the same scenario (checking at the code level) ?!


Why are you again asking thhe same question, this is what it tells you, I just ran it:

System.out.println("running on JRE: " + System.getProperty("sun.arch.data.model"));

Open in new window



Output:


running on JRE: 32

Open in new window


So this is how you check it:

if(System.getProperty("sun.arch.data.model").equals("64")) {
//it is 64-bit

} else
{
// it is 32
}

because on aome old systems it may return unknnown for 32-bit,
but if itbreturns 64 then it is 64



As for_yan already discussed,to chk the JRE version u can use :

System.getProperty("sun.arch.data.model") // returns 32 for 32-bit and 64 for 64-bit
Avatar of CEHJ
>>
Great, NOW how do you check which JRE version is installed assuming the same scenario (checking at the code level) ?!
>>
System.out.println(System.getProperty("java.version"));

Open in new window

This guy is saying that System.getProperty("os.arch");  returns the bit-ness of Java Runtime which is running this code and not
the information about your overall system.

The way yyour question is posed - you want to identify the JRE which is running this java program
So if you really need to know the type of JRE which is running your code you can use

 System.getProperty("os.arch");  

or

System.getProperty("sun.arch.data.model") ;

On 64 bit machine you can run either 64-bit JRE or 32-bit JRE
Of course on 32-bit system - you cannot run 64-bit JRE.

Therore if

System.getProperty("sun.arch.data.model") returns "64"

that means that you are running 64-bit JRE on 64-bit machine

If

System.getProperty("sun.arch.data.model") returns "32"

it measn that JRE which runs this code is 32-bit
You cannot know if your machine is 32-bit or 64-bit based on this test



I'm wondering if standard

System.getProperty("os.name")

which is often used in java code to find the Operating System
will return different names on 64-bit and 32-bit systems?

I have only 32-bit machines, so I cannot test it


 
Avatar of snajalm

ASKER

For me System.getProperty("os.name") just returned "Windows 7"  and that's it!  PLEASE, I need a routine to find the ACTUAL "OPERATING SYSTEM" bit level on both mac and Windows systems!
It looks like it is more difficult to determine the actual 32-bit or 64-bit architecture of your machine
(in contrast to detemining the type of JRE which is running your code - which is simple - seee abvwe)
In the link which you posted they don't give any java solution, but rather recommend to use the C code
to do it.
Check what is returned on your system from dos comdline:

echo %PROCESSOR_ARCHITECTURE%
On my system:

  System.out.println( System.getenv("PROCESSOR_ARCHITECTURE"));

Open in new window


Output:

x86

Open in new window


Check what it returns on 64 bit system
Avatar of snajalm

ASKER

How can I encode it inside my code??!
I showed you  - this is in java code:

System.getenv("PROCESSOR_ARCHITECTURE");

but check what it returns on your machine.
Avatar of snajalm

ASKER

it returns "AMD64", however in order to test it I'll remove JRE 64bit version and install the 32bit version to see if I'm getting the right OS bit level!
Sure you can try it using both JRE 64 and JRE 32.
I don't think you ned to remove - I think 32-bit and 64-bit JRE can coexist on your machine ansd you
can use one or another depending to which /bin folder will be the fisrt in your PTH - the one form 64-bit or from 32-bit installation.

I'm pretty sure this test will be independent of the JRE which you are running.
It just returns the same thing which it will returtn for you in dos window

echo %PROCESSOR_ARCHITECTURE%

and this is of course indeoendent of which JRE you are rnning, as this sysem variable has nothing to do with any JRE
Avatar of snajalm

ASKER

No, it still returns the bit values of the JRE (x86)!  This is not good!  Could you please let me know if there are any firm and bullet proof way of doing this!  Need this urgently!
Sorry I don't understand that.

Please open dos window (irrespective of any JRE) and type

echo %PROCESSOR_ARCHITECTURE%


and let me know what it prints to you in the dos window.
Avatar of snajalm

ASKER

It returns "AMD64" but in the context of System.getenv("PROCESSOR_ARCHITECTURE"); it returns something else!  I too am shocked by this behaviour!
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


try this on your command line:

reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" /v PROCESSOR_ARCHITECTURE

if that works then  you can try to use this command string form java code:

Rntime.getRuntime().exec("reg query \"HKLM\System\CurrentControlSet\Control\Session Manager\Environment\" /v PROCESSOR_ARCHITECTURE");

And read output from this command

Avatar of snajalm

ASKER

Thanks