MeFisto94 / lwjgl

[LEGACY] LWJGL 2.X - The Lightweight Java Game Library.
http://legacy.lwjgl.org/
2 stars 1 forks source link

Java 8 compatibilities #2

Open Ali-RS opened 1 year ago

Ali-RS commented 1 year ago

Hi @MeFisto94

I am getting this exception when using java 8

java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer;
    at org.lwjgl.MemoryUtil.encode(MemoryUtil.java:285)
    at org.lwjgl.MemoryUtil.encode(MemoryUtil.java:243)
    at org.lwjgl.MemoryUtil.encodeUTF8(MemoryUtil.java:216)
    at org.lwjgl.opengl.LinuxDisplay.setClassHint(LinuxDisplay.java:796)
    at org.lwjgl.opengl.LinuxDisplay.createWindow(LinuxDisplay.java:500)
    at org.lwjgl.opengl.Display.createWindow(Display.java:306)
    at org.lwjgl.opengl.Display.create(Display.java:848)
    at org.lwjgl.opengl.Display.create(Display.java:797)
    at com.jme3.system.lwjgl.LwjglDisplay.createContext(LwjglDisplay.java:138)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:120)
    at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:221)
    at java.lang.Thread.run(Thread.java:750)

See this for more details:

https://stackoverflow.com/questions/61267495/exception-in-thread-main-java-lang-nosuchmethoderror-java-nio-bytebuffer-flip

I tried approach 1 described above by adding release="8" when building with java 11 but I did not succeed. The error still happens.

Another approach is to find and cast all ByteBuffers to Buffer before calling flip(), limit(),... methods. Seems we should update lots of code this way.

Can we use java 8 to build the lwjgl.jar and only use java 11 for building natives?

Or do you know a better solution?

MeFisto94 commented 1 year ago

So without reading the stackoverflow link, I can tell you we had the exact same problem, but just the opposite when switching to Java 11. The ABI/API has changed, and as such both versions aren't really interchangeable, as far as I understand.

I'd need to look into the link (and you can do something by splitting into modules/classes that you load dynamically that implement an interface), but the ultimate solution would be to use the regular lwjgl2 release for java <= 8 and to use my fork starting with java 11 anyway.

Edit: note that this may also apply to jme itself.

Ali-RS commented 1 year ago

but the ultimate solution would be to use the regular lwjgl2 release for java <= 8 and to use my fork starting with java 11 anyway.

Note that If I build the lwjgl.jar with java 8 (using jars ant task) and only use the natives from your fork it will work fine on both java 8 and 11+.

Ali-RS commented 1 year ago

Please see: https://github.com/MeFisto94/lwjgl/compare/master...Ali-RS:lwjgl:master

I updated github workflow to build it with java 8 and disabled compiling natives and instead uploaded the precompiled linux natives. The liblwjgl64.so is compiled with java 11 I grabbed from your fork.

Also applied lwjgl2-primarymonitor-patch-xrandr made by @riccardobl that partially fix https://github.com/jMonkeyEngine/jmonkeyengine/issues/947 (we still have the AWT issue sometimes returning incorrect display mode that needs to be settled as well. see https://github.com/jMonkeyEngine/jmonkeyengine/issues/1890#issuecomment-1372630501).

In the tests I did it works fine with java 8 and 11+. (it resolves https://github.com/jMonkeyEngine/jmonkeyengine/issues/1247 and https://github.com/jMonkeyEngine/jmonkeyengine/issues/1215)

Now we need to decide how to ship it to maven central in org.jmonkeyengine.lwjgl and move this to JME GitHub.