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

前端 未结 9 1167
离开以前
离开以前 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:27

    I'm using NSIS and Launch4j to wrap a Java Desktop app. So I need not only to detect any JRE, but the one Launch4j will find with its search algorithm. The only approach that made sense is to run a short Java program within the NSIS installer. Here's the Java:

    
        public class DetectJVM {
            private static final String keys [] = {
                "sun.arch.data.model",
                "com.ibm.vm.bitmode",
                "os.arch",
            };
            public static void main (String [] args) {
                boolean print = args.length > 0 && "-print".equals(args[0]);
                for (String key : keys ) {
                    String property = System.getProperty(key);
                    if (print) System.out.println(key + "=" + property);
                    if (property != null) {
                        int errCode = (property.indexOf("64") >= 0) ? 64 : 32;
                        if (print) System.out.println("err code=" + errCode);
                        System.exit(errCode);
                    }
                }
            }
        }
    
    

    Wrap this with Launch4J. Use the GUI header type but also set to true. Otherwise the error code will be lost. (I put all this in my Netbeans Ant build script.

    Here's the matching NSIS code that uses it:

    
    File ... ; unpack files including detectjvm.exe.
    ClearErrors
    ExecWait '"$INSTDIR\detectjvm.exe"' $0
    IfErrors DetectExecError
    IntCmp $0 0 DetectError DetectError DoneDetect
    DetectExecError:
        StrCpy $0 "exec error"
    DetectError:
        MessageBox MB_OK "Could not determine JVM architecture ($0). Assuming 32-bit."
        Goto NotX64
    DoneDetect:
    IntCmp $0 64 X64 NotX64 NotX64
    X64:
        File  ... 64-bit AMD DLLs.
        Goto DoneX64
    NotX64:
        File ... 32-bit x86 DLLs.
    DoneX64:
    Delete $INSTDIR\detectjvm.exe
    
    

    This has worked fine on a very large variety of machines from WinXP with no SP through Vista and Win7 with all SPs, 32- and 64-bit.

    Note that in my NSIS script I'm using an existing package that checks to see if the JVM is installed and does that first, so the default 32-bit selection would only occur if something went badly wrong with the JVM install, in which case the set of DLLs you copy won't matter anyway.

    Hope this is helpful to somebody.

提交回复
热议问题