Java是否有任何可调用的API来知道进程或.exe文件是32位还是64位? - 不是您的代码正在运行的JVM如何确定Java中是否有其他进程或可执行文件是32位还是64位
回答
没有用于确定外部进程是32位还是64位的标准Java API。
如果您想这样做,您需要使用本机代码或调用外部实用程序来执行此操作。在这两种情况下,解决方案可能都是平台特定的。下面是一些可能的(特定平台)导致:
(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的情况下,解决方案涉及到测试“.exe”文件而不是正在运行的进程,所以你需要能够首先确定相关的“.exe”文件...)
谢谢Stephen! – Mango
对于Windows,我总是查询系统环境变量“PROCESSOR_ARCHITECTURE”。如果它返回x86,那么您正在运行32位Windows。 – TheBlastOne
@TheBlastOne - 但这是解决一个不同的问题......确定什么架构被用于这个过程。 –
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 SuperUser或The Windows HPC Team Blog。
感谢您的建议。我已经尝试过了,它在Visual Studio CMD中有效。 – Mango
我为Windows编写了一个Java的方法,它看起来像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 + ".");
}
}
主要方法输出如下:
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.
- 1. 确定可执行文件(或库)是32位还是64位
- 2. 确定可执行文件(或库)是32位还是64位(在Windows上)
- 3. 确定C#中的可执行文件是64位还是32位
- 4. 如何以编程方式确定特定进程是32位还是64位
- 5. 如何使用Java API确定文件是32位还是64位?
- 6. 如何从句柄中确定进程是32位还是64位?
- 7. 如何确定进程ID(PID)是32位还是64位应用程序?
- 8. 使用WMI,我如何确定远程进程是32位还是64位?
- 9. 无论系统是32位还是64位,int都是32位还是64位?
- 10. 有没有办法检查进程是64位还是32位?
- 11. 如何确定System.Diagnostics.Process是32位还是64位?
- 12. 检查正在运行的进程是32位还是64位
- 13. 确定Java安装是32位还是64位的Windows bat文件?
- 14. 确定是否编译ELF对象是32位或64位
- 15. WMI和Win32_Process - 确定是32位还是64位?
- 16. 确定当前的PowerShell Process是32位还是64位?
- 17. 确定iOS设备是32位还是64位
- 18. gcc如何确定默认情况下是否生成32位或64位可执行文件?
- 19. 如何判断我写的程序是32位还是64位?
- 20. 如何检测Cocoa应用程序是32位还是64位?
- 21. 如何判断应用程序是64位还是32位?
- 22. 我如何检查编译的二进制文件是32位还是64位?
- 23. 如何确定操作系统在Go中是32位还是64位?
- 24. 如何确定Python中的数字是32位还是64位整数?
- 25. 无论系统是32位还是64位,uint均为32位?
- 26. 查找ipa文件是32位还是64位。 (macos/unix)
- 27. 如何在PowerShell中检查要安装的可执行文件是32位还是64位?
- 28. 如何确定我的操作系统是32位还是64位?
- 29. 如何确定在Windows上安装的库是32位还是64位?
- 30. 如何检测,如果CPU是32位还是64位
要查看是否一些*其他*过程是32或64位?或者检查运行的JVM是否是32/64位? – nneonneo
要在Windows中查看其他进程,而不是JVM。 – Mango
我发现这是非常相似的... http://stackoverflow.com/a/9783522 – Mango