JDK-8123255 : static block setDefaultPlatformUserAgentStylesheet() in PlatformImpl causes IllegalStateException
  • Type: Bug
  • Component: javafx
  • Sub-Component: application-lifecycle
  • Affected Version: 8
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2013-10-30
  • Updated: 2015-06-17
  • Resolved: 2013-11-12
Related Reports
Duplicate :  
Description
I have a class that extends javafx.scene.control.Control. In the Control class there's a static block that calls another static block in PlatformImpl that causes an IllegalStateException.

Because it's a static block it will always (you never know exactly when actually) be executed before the toolkit can be started. This makes it impossible to run any javafx application.

Tested on 8b113 & 8b106

Caused by: java.lang.IllegalStateException: Toolkit not initialized
	at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:272)
	at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:267)
	at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:609)
	at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:571)
	at javafx.scene.control.Control.<clinit>(Control.java:81)
	... 26 more
Comments
After the fix for RT-28754 which is in b113, the toolkit will be initialized before the main method is called. I ran your sample program and verified this. So I am closing this as a duplicate. Can you please retest with b113 or later?
12-11-2013

I realise that is the problem. However the static method is called from a static block in the Control class. I, as a user, have no control when the static block is executed. This might be when the Control class is loaded which can be well before the toolkit will be initialised. And there is no way that I can influence this behaviour. Imagine this use case: //given A Control //when The Control's class is loaded before the Application launch is called. Don't assume the application.launch it will happen in the main method. (= bad assumption, think dynamic frameworks and the like.) //then Then the needed control initialisation should take place as soon as the javafx toolkit is started. //current an exception is thrown The reason I filed this bug is because the use case is exactly the behaviour I get (but do not want) when using Javafx in a DI framework (like google guice) where I have no control when the application.launch is called and classes are loaded before objects of them are initialised.
12-11-2013

Reopened, as I don't think this should be 'as designed' (see argument comments).
12-11-2013

If PlatformImpl.setDefaultPlatformUserAgentStylesheet() is called within the start method rather than main, this should work fine. The problem is that in main the JavaFX toolkit has not been initialised (whereas in start it has).
11-11-2013

Example code added
01-11-2013

import com.sun.javafx.application.PlatformImpl; import javafx.application.Application; import javafx.stage.Stage; public class Example extends Application { public static void main(String[] args) { //this is called from a static block in javafx.scene.control.Control PlatformImpl.setDefaultPlatformUserAgentStylesheet(); Application.launch(args); } @Override public void start(final Stage primaryStage) throws Exception { } }
31-10-2013

Please attach a simple test application that demonstrates this.
30-10-2013