JDK-8211754 : FXCanvas initialization causes error when using multiple classloaders
  • Type: Bug
  • Component: javafx
  • Sub-Component: other
  • Affected Version: openjfx11
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: x86_64
  • Submitted: 2018-10-05
  • Updated: 2018-11-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Description
ADDITIONAL SYSTEM INFORMATION :
os.arch: amd64, Windows 10, OpenJDK 11, OpenJFX 11 SDK

A DESCRIPTION OF THE PROBLEM :
The error occurs if you have embedded JavaFX e.g. in a Eclipse application where each plugin has it's own classloader. When FXPanels are initialized in different Eclipse plugins (classloaders) the following error occurs:

Caused by: java.lang.IllegalStateException: Toolkit already initialized
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:182)
	at javafx.graphics/javafx.application.Platform.startup(Platform.java:101)
	at javafx.embed.swt.FXCanvas.lambda$initFx$4(FXCanvas.java:354)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:429)
	at javafx.embed.swt.FXCanvas.initFx(FXCanvas.java:353)
	at javafx.embed.swt.FXCanvas.<clinit>(FXCanvas.java:271)
	... 154 more

The Problems was already analysed in the OpenJFX mailing list by Kevin Rushforth. A possible solution was posted, too, see:

http://mail.openjdk.java.net/pipermail/openjfx-dev/2018-October/022610.html

REGRESSION : Last worked in version 11

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a JavaFX application (e.g. a Eclipse RCP) with different classloaders (e.g. Eclipse plugins) and  in the different context initialize a JavaFX panel

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
An exception will be thrown that the toolkit was already initialized.


ACTUAL -
Here the error message of the initialization:

Caused by: java.lang.IllegalStateException: Toolkit already initialized
	at javafx.graphics/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:182)
	at javafx.graphics/javafx.application.Platform.startup(Platform.java:101)
	at javafx.embed.swt.FXCanvas.lambda$initFx$4(FXCanvas.java:354)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:429)
	at javafx.embed.swt.FXCanvas.initFx(FXCanvas.java:353)
	at javafx.embed.swt.FXCanvas.<clinit>(FXCanvas.java:271)
	... 154 more

CUSTOMER SUBMITTED WORKAROUND :
The Problems was already analysed in the OpenJFX mailing list by Kevin Rushforth. A possible solution was posted, too, see:

http://mail.openjdk.java.net/pipermail/openjfx-dev/2018-October/022610.html

FREQUENCY : always



Comments
I still come to the conclusion that nothing in JavaFX is designed be loaded multiple times by different classloaders and that the FXCanvas just worked by accident as Kevin said. There are multiple solutions Marcel could apply: * Keep his current strategy of Bundle-ClassPath but make just one in one bundle (ideally this one bundle just does the exposing of FXCanvas) * Use the Adaptor Hook provided by e(fx)clipse The only caveat is that both solutions can only be applied if you run on Equinox (they won't work eg on Felix) but I'm fairly sure all people who use SWT also use Equinox. I'll let that sink for a few days but currently I lean towards WONTFIX.
05-10-2018

Something definately broken (not fully working) if there are multiple FXCanvas classes is DND because a darg start (in javafx) will NOT update all other FXCanvas instances in the updateDropTarget() method!
05-10-2018

Ok I'll take a look.
05-10-2018

[~tschindl] I agree with you. This seems like something that may have just worked by accident in JDK 8u. It certainly could be the case that we fix this one problem and then something else breaks, either right away or later. I'll assign the bug to you since it is in FXCanvas, so you can decide whether / how to fix it.
05-10-2018

Before fixing I think we need to define if we really want to support loading multiple FXCanvas-Classes. As I said on the mailing list there are other statics in the code holding stuff so are we certain they work appropriately if there are multiple FXCanvas-Classes around?
05-10-2018