我想知道安装在用户机器上的 Java 运行时是否支持 32 位和 64 位,并且我想从 C 中执行此操作。我曾认为类似以下内容可以解决问题:
检测 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");
}
检测 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");
}
不幸的是,如果用户运行 64 位系统,如果 C 代码编译为 32 位可执行文件,则仅检测到 32 位 java,如果将程序编译为 64 位 Java,则仅检测到64 位程序。