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
-
To see if some *other* process is 32 or 64 bits? Or to check if the running JVM is 32/64 bits? – nneonneo Aug 06 '13 at 00:27
-
To see other process in Windows, not JVM. – Mango Aug 06 '13 at 00:28
-
I found that this is quite similar... http://stackoverflow.com/a/9783522 – Mango Aug 06 '13 at 03:01
-
@Mango - it is similar, but it only covers half of your Question ... and none of its original version. – Stephen C Aug 07 '13 at 03:35
-
1I also found this, http://msdn.microsoft.com/library/windows/hardware/gg463125, it describe the structure of executable (image) files. I am able to extract the bit size of the exe file (0x8664 is 64bits and 0x14c is 32 bits) by putting the exe file in fileInputStream in java and looking for the PE (Portable Executable, PE). The PE is consuming 4 bytes of data, which is"P E null null". The bit size of exe file is right after the 4 bytes of PE. – Mango Aug 07 '13 at 06:18
3 Answers
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
(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 ...)
-
For Windows, I´ve always quieried the system environment variable "PROCESSOR_ARCHITECTURE". If it returns x86, you are running 32-bit Windows. – TheBlastOne Aug 06 '13 at 00:43
-
@TheBlastOne - But that's solving a different problem ... determining what architecture is being used for THIS process. – Stephen C Aug 06 '13 at 00:45
-
Yeah, right...just mentioning. +1´d, and thought this might be useful. – TheBlastOne Aug 06 '13 at 00:46
-
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.
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.
-
Thanks for the suggestion. I have tried it out, it's works in Visual Studio CMD. – Mango Aug 06 '13 at 01:21