mojohaus / nbm-maven-plugin

moved to https://github.com/apache/incubator-netbeans-mavenutils/
http://www.mojohaus.org/nbm-maven-plugin/
24 stars 19 forks source link

Java 9 and 32 vs 64 bit #28

Open phansson opened 6 years ago

phansson commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.