lwjglgamedev / lwjglbook-leg

Source code of the chapters of the book 3D Game Development with LWJGL 3
https://ahbejarano.gitbook.io/lwjglgamedev/
Apache License 2.0
560 stars 202 forks source link

Java 9 compatibility #45

Closed Baskiney closed 6 years ago

Baskiney commented 6 years ago

Hey,

it seems that the code is not working properly anymore with the new Java 9 version. They seem to have changed the behaviour of input-streams, so that compiling the input of the shader-files will throw an NPE under Java 9. Java 8 still works fine though.

Is there any chance of an update on this?

lwjglgamedev commented 6 years ago

Sure! Can you point to the first chapter that you get the error?

Baskiney commented 6 years ago

The first occurrence of this would be in chapter 04. Although I personally tested it in chapter 06.

Thanks!

SoraCasus commented 6 years ago

It's the way you load the shader code in from the file. Prior to Java 9 using Utils.class.getResourceAsStream() would have worked fine. Now with Java 9, you can't call getResourceAsStream from a static context.

lwjglgamedev commented 6 years ago

Yes, the modules introduced in Java 9 have modified also teh way resources are loaded. I've just uploaded a version that supports Java 9. I will use that version as the default one, so I had also to move to IntelliJ since the other IDEs have some issues with Java 9.

Thanks por pointing the issue !

Baskiney commented 6 years ago

Hello,

first of all thanks for the quick responses but I still could not get it to compile on my System. Which is interesting, because it seems to have worked on yours when you updated it. I have found that modifying the InputStream to use the ContextClassLoader for the current Thread works for me personally.

If anyone is having issues like this after the update, he/she should try reading the Resources the following way:

    try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
         Scanner scanner = new Scanner(in, "UTF-8")) {
        result = scanner.useDelimiter("\\A").next();
    }

Any idea why this happens? Hope this helps!