简体   繁体   English

如何在Java中确定另一个进程或可执行文件是32位还是64位

[英]How to determine, in Java, whether another process or executable is 32-bit or 64-bit

Does Java has any API to call that can know whether a process or an .exe file is 32-bit or 64-bits? Java是否有任何可以调用的API,可以知道进程或.exe文件是32位还是64位? - not the JVM in which your code is running - 而不是运行代码的JVM

There is no standard Java API for determining whether an external process is 32 or 64 bit. 没有标准的Java API来确定外部进程是32位还是64位。

If you wanted to do this, you would either need to use native code, or call an external utility to do this. 如果您想这样做,您可能需要使用本机代码,或者调用外部实用程序来执行此操作。 The solution is likely to be platform specific in both cases. 在这两种情况下,解决方案可能都是平台特定的。 Here are some possible (platform specific) leads: 以下是一些可能的(平台特定的)潜在客户:

(Note that in the Windows cases, the solution involves testing the ".exe" file rather than the running process, so you need to be able to determine the relevant ".exe" file first ...) (请注意,在Windows情况下,解决方案涉及测试“.exe”文件而不是正在运行的进程,因此您需要能够首先确定相关的“.exe”文件...)

Java does not come with any standard API that allows you to determine whether a program is 32-bit or 64-bit. Java没有任何标准API,可以让您确定程序是32位还是64位。

On Windows, however, you can use (assuming you got the platform SDK installed) dumpbin /headers . 但是,在Windows上,您可以使用(假设您安装了平台SDK) dumpbin /headers Calling this will yield all sorts of information about the file in question, thereamong information about whether the file is a 32-bit or 64-bit. 调用此方法将产生有关该文件的各种信息,其中包含有关该文件是32位还是64位的信息。 In the output, on 64-bit , you'd something like 在输出中,在64位上 ,你会有类似的东西

8664 machine (x64)

On 32-bit , you'd get something like 32位 ,你会得到类似的东西

14C machine (x86)

You can read more about other ways of determining if an application is 64-bit on SuperUser or on The Windows HPC Team Blog . 您可以在SuperUserWindows HPC团队博客上阅读有关确定应用程序是否为64位的其他方法的更多信息。

I wrote a method in Java for Windows, which looks at the same headers as dumpbin without having to have it on the system (based on this answer ). 我在Java for Windows中编写了一个方法,该方法查看与dumpbin相同的头文件,而不必在系统上使用它(基于此答案 )。

/** 
 * Reads the .exe file to find headers that tell us if the file is 32 or 64 bit.
 * 
 * Note: Assumes byte pattern 0x50, 0x45, 0x00, 0x00 just before the byte that tells us the architecture.
 * 
 * @param filepath fully qualified .exe file path.
 * @return true if the file is a 64-bit executable; false otherwise.
 * @throws IOException if there is a problem reading the file or the file does not end in .exe.
 */
public static boolean isExeFile64Bit(String filepath) throws IOException {
    if (!filepath.endsWith(".exe")) {
        throw new IOException("Not a Windows .exe file.");
    }

    byte[] fileData = new byte[1024]; // Should be enough bytes to make it to the necessary header.
    try (FileInputStream input = new FileInputStream(filepath)) {
        int bytesRead = input.read(fileData);
        for (int i = 0; i < bytesRead; i++) {
            if (fileData[i] == 0x50 && (i+5 < bytesRead)) {
                if (fileData[i+1] == 0x45 && fileData[i+2] == 0 && fileData[i+3] == 0) {
                    return fileData[i+4] == 0x64;
                }
            }
        }
    }

    return false;
}

public static void main(String[] args) throws IOException {
    String[] files = new String[] {
            "C:/Windows/system32/cmd.exe",                           // 64-bit
            "C:/Windows/syswow64/cmd.exe",                           // 32-bit
            "C:/Program Files (x86)/Java/jre1.8.0_73/bin/java.exe",  // 32-bit
            "C:/Program Files/Java/jre1.8.0_73/bin/java.exe",        // 64-bit
            };
    for (String file : files) {
        System.out.println((isExeFile64Bit(file) ? "64" : "32") + "-bit file: " + file + ".");
    }
}

The main method outputs the following: 主要方法输出如下:

64-bit file: C:/Windows/system32/cmd.exe. 
32-bit file: C:/Windows/syswow64/cmd.exe. 
32-bit file: C:/Program Files (x86)/Java/jre1.8.0_73/bin/java.exe. 
64-bit file: C:/Program Files/Java/jre1.8.0_73/bin/java.exe.

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

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