【问题标题】:How to determine, in Java, whether another process or executable is 32-bit or 64-bit如何在 Java 中确定另一个进程或可执行文件是 32 位还是 64 位
【发布时间】:2013-08-06 20:31:42
【问题描述】:

Java 是否有可以调用的 API 可以知道进程或 .exe 文件是 32 位还是 64 位? - 不是运行代码的 JVM

【问题讨论】:

  • 查看某个 other 进程是 32 位还是 64 位?或者检查正在运行的 JVM 是否为 32/64 位?
  • 查看 Windows 中的其他进程,而不是 JVM。
  • 我发现这很相似...stackoverflow.com/a/9783522
  • @Mango - 它很相似,但它只涵盖了你的问题的一半......并且没有原始版本。
  • 我还发现了这个,msdn.microsoft.com/library/windows/hardware/gg463125,它描述了可执行(图像)文件的结构。我可以通过将 exe 文件放在 java 中的 fileInputStream 中并查找 PE(Portable Executable,PE)来提取 exe 文件的位大小(0x8664 是 64 位,0x14c 是 32 位)。 PE正在消耗4字节的数据,即“P E null null”。 exe文件的位大小正好在PE的4字节之后。

标签: java 32bit-64bit


【解决方案1】:

没有用于确定外部进程是 32 位还是 64 位的标准 Java API。

如果您想这样做,您要么需要使用本机代码,要么调用外部实用程序来执行此操作。在这两种情况下,解决方案都可能是特定于平台的。以下是一些可能的(特定于平台的)线索:

(注意,在Windows情况下,解决方案是测试“.exe”文件而不是正在运行的进程,因此您需要能够首先确定相关的“.exe”文件...)

【讨论】:

  • 对于 Windows,我总是查询系统环境变量“PROCESSOR_ARCHITECTURE”。如果返回 x86,则您运行的是 32 位 Windows。
  • @TheBlastOne - 但这解决了一个不同的问题......确定这个过程使用什么架构。
  • 是的,对...只是提一下。 +1'd,并认为这可能有用。
  • 其实是一个exe文件。可能是 dumpbin /headers 将有助于做到这一点
【解决方案2】:

我在 Java 中为 Windows 编写了一个方法,它查看与 dumpbin 相同的标头,而不必在系统上安装它(基于 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 + ".");
    }
}

main方法输出如下:

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.

【讨论】:

    【解决方案3】:

    Java 不附带任何标准 API,可让您确定程序是 32 位还是 64 位。

    但是,在 Windows 上,您可以使用(假设您安装了平台 SDK)dumpbin /headers。调用它会产生有关文件的各种信息,其中包括有关文件是 32 位还是 64 位的信息。在输出中,在 64 位 上,您会看到类似

    8664 machine (x64)
    

    32 位上,你会得到类似的东西

    14C machine (x86)
    

    您可以阅读更多有关determining if an application is 64-bit on SuperUserThe Windows HPC Team Blog 的其他方式的信息。

    【讨论】:

    • 感谢您的建议。我已经试过了,它在 Visual Studio CMD 中工作。
    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 2012-10-20
    • 2013-02-23
    • 2011-04-04
    • 1970-01-01
    • 2012-01-25
    • 2011-04-19
    • 2010-12-29
    相关资源
    最近更新 更多