Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

w00tmaster

macrumors regular
Original poster
Jun 21, 2004
165
0
Long story short I need to know what kind of machine I am running on for a program I am writing. How can I tell using JAVA if I am on an Intel machine or a PPC machine? The code will only be running on macs, so OS specific things wouldn't be the end of the world.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I've only written maybe 15 lines of Java in my life, but a quick Google reveals the ByteOrder class, and the static method nativeOrder().

So, from that documentation, something around these lines:

Code:
if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
{
    // PPC
}
else
{
    // Intel
}
 

therevolution

macrumors 6502
May 12, 2003
468
0
I've got a better way:

Code:
boolean isPPC = System.getProperty("os.arch").equals("ppc");
boolean isIntel = System.getProperty("os.arch").equals("i386");
 

therevolution

macrumors 6502
May 12, 2003
468
0
a small enhancement, just in case

Code:
boolean isPPC = System.getProperty("os.arch").trim().equalsIgnoreCase("ppc");
boolean isIntel = System.getProperty("os.arch").trim().equalsIgnoreCase("i386");


;)

Paranoid, are we? :p But yes, things like that are always good.
 

therevolution

macrumors 6502
May 12, 2003
468
0
Also, I have no idea if the values are always lowercase.
They are, and I imagine they wouldn't suddenly change case for fear of breaking brittle code like mine. ;) Nah, if I was writing production code, I would have made the same checks. I was just trying to keep the code easy-to-understand for the OP.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I've got a better way:

Code:
boolean isPPC = System.getProperty("os.arch").equals("ppc");
boolean isIntel = System.getProperty("os.arch").equals("i386");

As I'm not a Java programmer, I'm curious as to why this is better? Does the other method actually do the bitwise operation math to determine the endian, and this method just pull a value?
 

mkrishnan

Moderator emeritus
Jan 9, 2004
29,776
15
Grand Rapids, MI, USA
As I'm not a Java programmer, I'm curious as to why this is better? Does the other method actually do the bitwise operation math to determine the endian, and this method just pull a value?

Well, the Endian method is sloppy...it exploits a coincidence (that the two architectures use different endianness). It's better to go to a value that's actually there for this very purpose... you tend to never know when being sloppy like that will backfire on you....

But so random question... are all of the 64-bit Core 2 / Merom / Conroe architecture processors considered to be of architecture "i386" by the operating system?
 

Edot

macrumors 6502
Jan 29, 2002
432
0
NJ
As I'm not a Java programmer, I'm curious as to why this is better? Does the other method actually do the bitwise operation math to determine the endian, and this method just pull a value?

The endianness of PPC processors can be changed. Not sure if that effects the call to ByteOrder.nativeOrder() but as has been said, it is a bit risky.
 

therevolution

macrumors 6502
May 12, 2003
468
0
But so random question... are all of the 64-bit Core 2 / Merom / Conroe architecture processors considered to be of architecture "i386" by the operating system?

It returns "i386" on my Core 2 Duo MacBook Pro. I would assume it returns the same thing for the other processors you mentioned as well...
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
As I'm not a Java programmer, I'm curious as to why this is better? Does the other method actually do the bitwise operation math to determine the endian, and this method just pull a value?

It is a very simple principle: If you want a question answered, you should ask exactly the right question. The original poster wanted to know if Java is running on a PowerPC or an Intel chip - and that is what the code checks. Run the same code on an ARM chip and it will give the correct answer: It is not running on a PowerPC chip and not on an Intel chip.

The other code told you that Java was running on a bigendian or littleendian machine. You can draw conclusions from this, for example whether you are running on PowerPC or Intel. Run the code on an ARM chip, and the conclusion will be wrong.

(I bet that there is lots of code that was supposed to run on either Windows or Macintosh, but checked for Intel vs. PowerPC instead. Probably was fun fixing all that code for Intel Macs. Don't make assumptions. One day your assumptions will be wrong).
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
But so random question... are all of the 64-bit Core 2 / Merom / Conroe architecture processors considered to be of architecture "i386" by the operating system?

I think it will not tell you what processor is in your computer, it will tell you what kind of code is running. i386 usually stands for 32 bit intel code, and ia64 for 64 bit intel code. So apparently you are running a 32 bit Intel virtual machine.

PowerPC code running under Rosetta will usually think that it is running on a PowerPC, for example.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.