Determining if I'm on PPC or Intel mac in JAVA

Discussion in 'Mac Programming' started by w00tmaster, Dec 4, 2006.

  1. w00tmaster macrumors regular

    Joined:
    Jun 21, 2004
    #1
    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.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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
    }
     
  3. therevolution macrumors 6502

    Joined:
    May 12, 2003
    #3
    I've got a better way:

    Code:
    boolean isPPC = System.getProperty("os.arch").equals("ppc");
    boolean isIntel = System.getProperty("os.arch").equals("i386");
    
     
  4. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #4
    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");
    

    ;)
     
  5. therevolution macrumors 6502

    Joined:
    May 12, 2003
    #5
    Paranoid, are we? :p But yes, things like that are always good.
     
  6. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #6
    You'd be surprised how many times I have been burned by properties with whitespace...Although the likelihood of System props having them is slim to none. Also, I have no idea if the values are always lowercase.
     
  7. therevolution macrumors 6502

    Joined:
    May 12, 2003
    #7
    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.
     
  8. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    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?
     
  9. mkrishnan Moderator emeritus

    mkrishnan

    Joined:
    Jan 9, 2004
    Location:
    Grand Rapids, MI, USA
    #9
    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?
     
  10. Edot macrumors 6502

    Joined:
    Jan 29, 2002
    Location:
    NJ
    #10
    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.
     
  11. therevolution macrumors 6502

    Joined:
    May 12, 2003
    #11
    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...
     
  12. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #12
    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).
     
  13. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    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.
     

Share This Page