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??!
snajalmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
go to cmd dos window

make

echo %PATH%

and copy paste here the output
0
for_yanCommented:
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

0
snajalmAuthor Commented:
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!
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

for_yanCommented:
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.
0
snajalmAuthor Commented:
How would it be to identify the JRE installed at the code level??!
0
for_yanCommented:
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
}

0
sunu340Commented:
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..
0
snajalmAuthor Commented:
Great, NOW how do you check which JRE version is installed assuming the same scenario (checking at the code level) ?!
0
for_yanCommented:


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



0
sunu340Commented:
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
0
CEHJCommented:
>>
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

0
snajalmAuthor Commented:
0
for_yanCommented:
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



0
for_yanCommented:
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


 
0
snajalmAuthor Commented:
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!
0
for_yanCommented:
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.
0
for_yanCommented:
Check what is returned on your system from dos comdline:

echo %PROCESSOR_ARCHITECTURE%
0
for_yanCommented:
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
0
snajalmAuthor Commented:
How can I encode it inside my code??!
0
for_yanCommented:
I showed you  - this is in java code:

System.getenv("PROCESSOR_ARCHITECTURE");

but check what it returns on your machine.
0
snajalmAuthor Commented:
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!
0
for_yanCommented:
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
0
snajalmAuthor Commented:
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!
0
for_yanCommented:
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.
0
snajalmAuthor Commented:
It returns "AMD64" but in the context of System.getenv("PROCESSOR_ARCHITECTURE"); it returns something else!  I too am shocked by this behaviour!
0
for_yanCommented:

read this:
http://stackoverflow.com/questions/1738985/why-processor-architecture-always-returns-x86-instead-of-amd64

You may be getting the wrong environment variable. If your application is a 32-bit application running on a 64-bit OS the process version of this environment variable will return x86. If you want to find the architecture of the machine, you can grab it from the following registry entry:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE

Or if you're using .NET, then you can use this call to get it:

string arch = System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE",
  EnvironmentVariableTarget.Machine);

From the command line you can try this:

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:


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

0
snajalmAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.