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

百般思念 提交于 2020-01-24 08:26:28

问题


Does Java has any API to call that can know whether a process or an .exe file is 32-bit or 64-bits? - not the JVM in which your code is running


回答1:


There is no standard Java API for determining whether an external process is 32 or 64 bit.

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:

  • (OSX) Is there a way to check if process is 64 bit or 32 bit?

  • (Linux) https://unix.stackexchange.com/questions/12862/how-to-tell-if-a-running-program-is-64-bit-in-linux

  • (Windows) http://blogs.technet.com/b/windowshpc/archive/2009/03/27/how-to-tell-if-a-exe-file-is-a-32-bit-or-64-bit-application-using-dumpbin.aspx

(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 ...)




回答2:


Java does not come with any standard API that allows you to determine whether a program is 32-bit or 64-bit.

On Windows, however, you can use (assuming you got the platform SDK installed) 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. In the output, on 64-bit, you'd something like

8664 machine (x64)

On 32-bit, you'd get something like

14C machine (x86)

You can read more about other ways of determining if an application is 64-bit on SuperUser or onThe Windows HPC Team Blog.




回答3:


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).

/** 
 * 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.


来源:https://stackoverflow.com/questions/18069940/how-to-determine-in-java-whether-another-process-or-executable-is-32-bit-or-64

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!