codecentric / NSMenuFX

Other
128 stars 27 forks source link

IllegalAccessError on OpenJFX 11 #29

Closed Glavo closed 3 years ago

Glavo commented 6 years ago
glavo@glavo:~/IdeaProjects/ClassViewer/build/libs$ java --module-path $OPENJFX11_SDK --add-modules javafx.controls -jar ClassViewer-4.0-release.jar 
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class de.codecentric.centerdevice.glass.AdapterContext (in unnamed module @0x125f5c1) cannot access class com.sun.javafx.tk.Toolkit (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.tk to unnamed module @0x125f5c1
    at de.codecentric.centerdevice.glass.AdapterContext.createContext(AdapterContext.java:34)
    at de.codecentric.centerdevice.glass.AdapterContext.getContext(AdapterContext.java:19)
    at de.codecentric.centerdevice.MenuToolkit.toolkit(MenuToolkit.java:57)
    at de.codecentric.centerdevice.MenuToolkit.toolkit(MenuToolkit.java:53)
    at de.codecentric.centerdevice.MenuToolkit.toolkit(MenuToolkit.java:49)
    at org.glavo.viewer.gui.Viewer.start(Viewer.java:18)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    ... 1 more
Exception running application org.glavo.viewer.gui.Viewer
Glavo commented 6 years ago

Oh, I solved this problem by adding Add-Exports: javafx.graphics/com.sun.javafx.tk to MANIFEST.MF in my application. Can this problem be solved in NSMenuFX?

0x4a616e commented 6 years ago

AFAIK, this approach will only work for the "main" jar, so it won't have any effect when added to NSMenuFX. So I'm afraid this cannot be easily solved in NSMenuFX, at least not by adding exports.

msgilligan commented 5 years ago

It would be nice to see a NSMenuFX update for Java 11+. It would be fine with me, by-the-way if it only supported Java 11 and later. I say this because I intend to always deliver JavaFX apps with bundled runtimes and am currently migrating to Java 11 and will likely move to newer versions rapidly.

vewert commented 5 years ago

I too am attempting to migrate an application to Java 11, and have run into this same problem. It would be great if NSMenuFX could be somehow updated to support Java11+.

0x4a616e commented 5 years ago

I guess the only way around this is to ship own native bindings to get direct access to the mac menu bar. This will also give us way more cool opportunities like adding a menu if the app has no active window or customising the menu on the app icon in the dock. I already have a more or less working version ready but it still requires some small adjustments to make it fully compatible with the old version. I think all required objective-c code has been written, so all that's missing is some java glue code. If anyone wants to speed this up a bit, help is always welcome ;) The biggest issue i'm currently aware of that needs some solution is when the menu should/can be set. If it is set too early, it'll get overwritten again by the JavaFX default one. I tried using WindowEvent.WINDOW_SHOWN but unfortunately that event is fired just before the window becomes visible. When I set the menu after receiving this event, the menu bar gets replaced again. For testing, i'm just using a small delay after receiving the event before setting the menu, but that does not seem like a good solution. If anyone has a good idea how to fix this (preferably without additional native bindings), i'd be happy to hear it :)

em-t commented 4 years ago

@0x4a616e Are you still working on this, or have you dropped it?

mipastgt commented 4 years ago

@0x4a616e

  1. Concerning the placement of the menu creation, I had some success with wrapping the menu creation into a Platform.runlater call but I do not know whether this works in general or just for me.
  2. Did you ever consider to fix the menu problems directly inside JavaFX. Nowadays this is not so difficult anymore and this sounds like the cleanest solution to me.
em-t commented 4 years ago

@0x4a616e

  1. Concerning the placement of the menu creation, I had some success with wrapping the menu creation into a Platform.runlater call but I do not know whether this works in general or just for me.
  2. Did you ever consider to fix the menu problems directly inside JavaFX. Nowadays this is not so difficult anymore and this sounds like the cleanest solution to me.

Thanks for the quick reply.

  1. You're probably right. I will start looking at that next. I was just checking if there's a quick fix - since our application already uses NSMenuFX, it would have required the least changes.
mipastgt commented 4 years ago

My reply was addressing @0x4a616e previous answer but you are of course also welcome to step in :-)

em-t commented 4 years ago

Whoopsie :D

0x4a616e commented 4 years ago

Unfortunately I haven't been able to find a solution for the aforementioned timing issue. Also I have to admit I don't have as much time as I'd like to have to address this problem. If someone finds a solution on when to overwrite the JavaFX default menu bar, i'll try to implement that in a timely manner. I already tried stackoverflow but people there also don't seem to have a good idea...

vewert commented 4 years ago

I finally got this working for my application. Through various trials, and checking the error messages, I found I needed to add the following --add-opens the the jvmArg:

"--add-opens", "javafx.graphics/com.sun.javafx.tk.quantum=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.javafx.tk=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.glass.ui=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.glass.ui.mac=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.javafx.menu=centerdevice.nsmenufx",
"--add-opens", "javafx.controls/com.sun.javafx.scene.control=centerdevice.nsmenufx"

I realize this is a bit of a work around to get around the IllegalAccessError, but it works (note: tried with Java13 and javaFX13)

0x4a616e commented 4 years ago

@vewert thanks for sharing!

msgilligan commented 4 years ago

With the tips from this issue, I was able to build a sample "Hello World" app into a standalone native-looking macOS app using jpackage via the Badass JLink Gradle Plugin I'm using Github Actions to do a matrix build and it works when built/bundled with both OpenJDK 11 and OpenJDK 13. You can view the build and download/test the artifacts here: https://github.com/ConsensusJ/SupernautFX/actions/runs/38586288

And here's the commit that added NSMenuFX: https://github.com/ConsensusJ/SupernautFX/commit/ab7b18a62a7a7bd24b1250273394127d78d19668

0x4a616e commented 3 years ago

Please refer to https://github.com/codecentric/NSMenuFX/issues/36 for recent updates. Those type of issues should be solved with the new major upgrade.

0x4a616e commented 3 years ago

Closing this as obsolete as i just published

<dependency>
  <groupId>de.jangassen</groupId>
  <artifactId>nsmenufx</artifactId>
  <version>3.0.0</version>
</dependency>