2

Trying to run server and client on my java project which consists of a boardgame made in CLI and GUI. Unfortunately I'm ending up in this error after adding username:

Exception in thread "Thread-0" java.lang.reflect.InaccessibleObjectException: Unable to make field private float javafx.scene.paint.Color.red accessible: module javafx.graphics does not "opens javafx.scene.paint" to module gson

Here is the total error:

Exception in thread "Thread-0" java.lang.reflect.InaccessibleObjectException: Unable to make field private float javafx.scene.paint.Color.red accessible: module javafx.graphics does not "opens javafx.scene.paint" to module gson
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:341)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:281)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:176)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:170)
    at gson@2.8.5/com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible(UnsafeReflectionAccessor.java:44)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:159)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at gson@2.8.5/com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at gson@2.8.5/com.google.gson.Gson.getAdapter(Gson.java:458)
    at gson@2.8.5/com.google.gson.Gson.fromJson(Gson.java:926)
    at gson@2.8.5/com.google.gson.Gson.fromJson(Gson.java:892)
    at gson@2.8.5/com.google.gson.Gson.fromJson(Gson.java:841)
    at gson@2.8.5/com.google.gson.Gson.fromJson(Gson.java:813)
    at project/controller.JsonUnwrapper.sendJson(JsonUnwrapper.java:99)
    at project/network.Socket.JsonRouterSocket.run(JsonRouterSocket.java:55)
    at java.base/java.lang.Thread.run(Thread.java:835)

Somehow seems like there's a conflict between javafx and GSON. The funny part is that I'm running the server and client using CLI(which doesn't need javafx) rather then GUI. It's supposed that Gson should return some info(colours included) but when called! I'm not even executing a command that should return me some oinfo!

The communication can be made with RMI or Sockets but the error persists on both of them!

Moreover, the project is being made by me and 2 other guys. One of them uses Linux and the other Windows 10 (as me) but yet none of them do have this issue!

fabian
  • 80,457
  • 12
  • 86
  • 114
Lol
  • 77
  • 1
  • 1
  • 9
  • not much we can do from afar, can we ;) Just guessing: try --add-opens .. (same as in error message) to your java the runtime parameters – kleopatra Jun 28 '19 at 10:42

1 Answers1

1

This types of errors occur because of the modularisation in java 9.

The error says that the gson module wanted to get access to a field of the class javafx.scene.paint.Color using reflection. In java 8 this works fine because reflection can be used to make even private fields public, but when using java 9 there are some new restrictions to refelction that lead to errors like this one.

Like explained here, in java 9 you can't use reflection to gain access to a type of a module, which is not exported by the module, so reflection (like used in gson) will no longer work.

A solutions to this problem could be one of the following:

  • Use java 8 and no modularisation (if possible for your project)
  • Use the command line option --add-opens which opens a package of a module, so reflection should be usable again
  • Open the module in the module-info.java file (which will not work in your case because you have no access to the module-info.java of javafx; just mentioned for completeness)
  • Add the libraries (javafx and gson) in the CLASSPATH instead of the MODULEPATH which will lead to a compatibility mode, where reflection should be possible too.

Because the two other developters you mentioned don't have this problem, I assume they use either java 8 or have put the libraries in the CLASSPATH, because this is just a change in the project setup and not in the code. So this would be an explenation why only you are facing this problem.

Tobias
  • 2,547
  • 3
  • 14
  • 29