简体   繁体   English

在Windows上的C中检测32/64位Java

[英]Detecting 32/64 -bit Java from within C on windows

I would like to find out if the Java runtime installed on a user's machine supports 32 and 64-bit, and I would like to do this from within C. I had thought that something like the following would do the trick: 我想了解用户计算机上安装的Java运行时是否支持32位和64位,并且我想从C语言中做到这一点。我曾想过,以下方法可以解决问题:

Detect 64-bit java: 检测64位Java:

int f=0
char *path = (char*) malloc(32768);
char out[1035]; 
FILE *fp;
if(f) fprintf(f,"Checking if 64-bit Java is available via the java command\n");
java64ok = 1;
strcpy(path,"java -d64 -version 2>&1");
fp = _popen(path, "r");
if (fp == NULL) {
    if(f) fprintf(f,"Failed to run command\n" );
}
if(fgets(out, sizeof(out), fp) != NULL){
    if(strncmp(out,"Error",5)==0){
        java64ok = 0;
    }
    while (fgets(out, sizeof(out), fp) != NULL) {}
}
if (feof(fp)){
    pclose( fp );
}
else{
    if(f) fprintf(f, "Error: Failed to read the pipe to the end.\n");
}       

Detect 32-bit Java: 检测32位Java:

if(f) fprintf(f,"Checking if 32-bit Java is available via the java command\n");
java32ok = 1;
strcpy(path,"java -d32 -version 2>&1");
fp = _popen(path, "r");
if (fp == NULL) {
    if(f) fprintf(f,"Failed to run command\n" );
}
if(fgets(out, sizeof(out), fp) != NULL){
    if(strncmp(out,"Error",5)==0){
        java32ok = 0;
    }
    while (fgets(out, sizeof(out), fp) != NULL) {}
}
if (feof(fp)){
    pclose( fp );
}
else{
    if(f) fprintf(f, "Error: Failed to read the pipe to the end.\n");
}   

Unfortunately, this appears that if the user is running a 64-bit system only the 32 bit java is detected if the C code is compiled as a 32 bit executable, and only the 64-bit Java is detected if the program is compiled as a 64-bit program. 不幸的是,这似乎是:如果用户运行的是64位系统,则将C代码编译为32位可执行文件时,仅检测到32位Java;如果将程序编译为32位可执行文件,则仅检测到64位Java。 64位程序。

With reasonable reliability, you can check %SYSTEMROOT%\\SysWOW64\\java.exe (which is where 32 bit java.exe resides on a 64 bit machine), and %SYSTEMROOT%\\System32\\java.exe (which is where 64 bit java resides on a 64 bit machine and also where 32 bit java resides on a 32 bit machine.) 以合理的可靠性,您可以检查%SYSTEMROOT%\\ SysWOW64 \\ java.exe(这是32位java.exe驻留在64位计算机上的位置)和%SYSTEMROOT%\\ System32 \\ java.exe(这是64位Java所在的位置)驻留在64位计算机上,并且32位Java驻留在32位计算机上。)

Use the Windows API function GetEnvironmentVariable to deduce %SYSTEMROOT%, or, to get started, hardcode to "C:\\Windows", escaping \\ as necessary. 使用Windows API函数GetEnvironmentVariable推断%SYSTEMROOT%,或者开始使用,将硬编码到“ C:\\ Windows”,并根据需要转义\\。

How you detect the version externally is platform dependant. 您如何从外部检测版本取决于平台。 I sugges you run a short Java program to tell you. 我建议您运行一个简短的Java程序来告诉您。

public class Bitness {
    public static void main(String... ignored) {
        System.out.println(is64Bit() ? "64" : "32");
    }

    private static boolean is64Bit0() {
        String systemProp;
        systemProp = System.getProperty("com.ibm.vm.bitmode");
        if (systemProp != null) {
            return "64".equals(systemProp);
        }
        systemProp = System.getProperty("sun.arch.data.model");
        if (systemProp != null) {
            return "64".equals(systemProp);
        }
        systemProp = System.getProperty("java.vm.version");
        return systemProp != null && systemProp.contains("_64");
    }
}

This way you can run java -cp {whatever} Bitness and it will tell you. 这样,您可以运行java -cp {whatever} Bitness ,它将告诉您。

the suggestion of Bathsheba is quite solid, though depending on how the PATH variable is defined on a system, it might be possible that another java.exe binary is found before %SYSTEMROOT%\\, so there is a chance that a different java version is used. Bathsheba的建议非常扎实,尽管取决于系统上PATH变量的定义方式,有可能在%SYSTEMROOT%\\之前找到另一个java.exe二进制文件,因此有可能使用其他Java版本用过的。

My suggestion would be to first parse PATH environment variable and find the first hit for java. 我的建议是先解析PATH环境变量,然后找到Java的第一个匹配项。 Second step would be to look at the COFF header of the java.exe file. 第二步是查看java.exe文件的COFF标头。 With the ImageNtHeader function, you can query the NT header and this contains the field FileHeader , which is a IMAGE_FILE_HEADER . 使用ImageNtHeader函数,您可以查询NT标头,其中包含字段FileHeader ,它是IMAGE_FILE_HEADER

Here's some example on how you would implement this: 以下是有关如何实现此操作的示例:
https://stackoverflow.com/a/4316804/435583 https://stackoverflow.com/a/4316804/435583

Hope this helps you out. 希望这可以帮助你。
Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM