Skip to content
This repository has been archived by the owner on Jan 21, 2019. It is now read-only.

Java 9 and 32 vs 64 bit #28

Open
phansson opened this issue Aug 27, 2017 · 3 comments
Open

Java 9 and 32 vs 64 bit #28

phansson opened this issue Aug 27, 2017 · 3 comments

Comments

@phansson
Copy link

The plugin's method for determining if the JVM is 32 bit or 64 bit will not work with Java 9.

In files RunNetBeansMojo.java and RunPlatformAppMojo.java the method is based on testing for the existence of

"jre\\lib\\amd64\\jvm.cfg"

This will for sure not work with Java 9.

(btw : the same code is duplicated between the two mojos, so maybe this is an opportunity for a little bit of refactoring?)

@mkleint
Copy link
Contributor

mkleint commented Aug 27, 2017

I currently don't have access to a windows machine, do you know what is the way to figure out if 32 or 64 bit are used on jdk9?

@phansson
Copy link
Author

phansson commented Aug 27, 2017

Sure. I don't think you can tell the difference any more on 32-bit vs 64-bit just by checking for the presence of a file as was the case prior to Java 9.

However, there's a another way:

Regardless of Java version the executable is bin\java.exe. This hasn't changed.

You can fairly easily determine if the executable is 32 or 64 bit by peeking into it.
This goes for any .exe on Windows: Open the file and read the two-byte short value starting at position 60 (0x3c). This value determines the byte location (offset) of the so-called PE signature. The PE signature is the sequence 'PE\0\0' (the letters P, E and two null bytes) and is thus always 4 bytes. The two bytes immediately following this is a short and is known as machine type in the MS specs for the PE format and this is exactly what will give you your information:

image

(I've highlighted the two values you need to check for, the rest you'll never encounter :-))

While this may seem technically complex (no, not really), it is fully document by MS, it is bullet proof, fast and forward and backward compatible. It is fast because you won't have to read much from the file, the info is somewhere in the PE header.

Remember to err on the side of caution.

Alternatively you can call the native function GetBinaryType using either JNA or JNI but I believe the above is simpler.

@phansson
Copy link
Author

phansson commented Aug 27, 2017

Have a look at the Gist I created: https://gist.github.com/phansson/3160a3f607fd887a2cdf031896115f5c

Feel free to copyright it in your own name or whatever.

You'll simply use it like this

    if (WinExeUtils.is64Bit(exeFile)) {

    }

It is quite simple what it does. No magic.

You can use this method for any version of Java.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants