How do I detect which kind of JRE is installed — 32bit vs. 64bit

前端 未结 9 1131
离开以前
离开以前 2020-11-27 03:47

During installation with an NSIS installer, I need to check which JRE (32bit vs 64bit) is installed on a system. I already know that I can check a system property \"su

相关标签:
9条回答
  • 2020-11-27 04:49

    There might be both 32 bit and 64 bit JVM's available on the system, and plenty of them.

    If you already have dll's for each supported platform - consider making a small executable which links and run so you can test if the platform supports a given functionality. If the executable links and run, you can install the corresponding shared libraries.

    0 讨论(0)
  • 2020-11-27 04:53

    If you have the path to the .exe you want to check, you can use this answer. Basically it just looks at the headers in the .exe file and tells you whether or not it is 64 or 32 bit on Windows.

    0 讨论(0)
  • 2020-11-27 04:54

    The following code checks the machineType field in any windows executable to determine if it is 32 or 64 bit:

    public class ExeDetect
    {
      public static void main(String[] args) throws Exception {
        File x64 = new File("C:/Program Files/Java/jre1.6.0_04/bin/java.exe");
        File x86 = new File("C:/Program Files (x86)/Java/jre1.6.0/bin/java.exe");
        System.out.println(is64Bit(x64));
        System.out.println(is64Bit(x86));
      }
    
      public static boolean is64Bit(File exe) throws IOException {
        InputStream is = new FileInputStream(exe);
        int magic = is.read() | is.read() << 8;
        if(magic != 0x5A4D) 
            throw new IOException("Invalid Exe");
        for(int i = 0; i < 58; i++) is.read(); // skip until pe offset
        int address = is.read() | is.read() << 8 | 
             is.read() << 16 | is.read() << 24;
        for(int i = 0; i < address - 60; i++) is.read(); // skip until pe header+4
        int machineType = is.read() | is.read() << 8;
        return machineType == 0x8664;
      }
    }
    

    Note that the code has been compacted for brevity...

    0 讨论(0)
提交回复
热议问题