30

I'm trying to make a simple mp3 play in the background of my program using the following:

Media med = new Media(getClass().getResource("intro.mp3").toExternalForm());
MediaPlayer mPlayer = new MediaPlayer(med);
mPlayer.play();

The intro.mp3 file is placed in the bin folder of my package, along with the other .class files.

The problem is that my program terminates with:

Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized

Full termination log is:

Device "Intel(R) HD Graphics Family" (\\.\DISPLAY1) initialization failed : 
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 8.15.10.2302

Exception in thread "main" java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:153)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:148)
    at javafx.application.Platform.runLater(Platform.java:52)
    at javafx.scene.media.MediaPlayer.init(MediaPlayer.java:450)
    at javafx.scene.media.MediaPlayer.<init>(MediaPlayer.java:365)
    at PokerApp.<init>(PokerApp.java:33)
    at PokerApp.main(PokerApp.java:105)

Anybody have any ideas as per the cause of the problem?

Jonathan
  • 20,053
  • 6
  • 63
  • 70
Dimitris Sfounis
  • 2,400
  • 4
  • 31
  • 46

4 Answers4

28

JavaFX performs "hidden" initialization on start. Running MediaPlayer doesn't trigger initialization.

The easiest ways to trigger it are:

  • have Application.launch() executed
  • have Application based program being run from jar packaged by fx ant tasks (e.g. built from Netbeans JavaFX project)
  • have JFXPanel started
  • call Platform.startup(Runnable) (Java 9+)
Slaw
  • 37,820
  • 8
  • 53
  • 80
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • 1
    Will try it out and mark the question as the correct one if it works after I get home. Merry christmas to you, mister Sergey, and thank you for your answers in both my questions. – Dimitris Sfounis Dec 25 '12 at 09:20
  • 1
    I placed a single JFXPanel fxPanel = new JFXPanel(); in my constructor and everything worked out fine. Is it recommended I use a JFXPanel instead of a JPanel or a Container when I use JavaFX with SWING? – Dimitris Sfounis Dec 26 '12 at 10:22
  • you can put fx nodes only into JFXPanel, so it's definitely recommended to use it if you want to have Swing app with JavaFX features. – Sergey Grinev Dec 26 '12 at 11:56
  • What would I do if, say, I was running headless? – Ruraj Aug 20 '15 at 10:56
  • I don't think JavaFX supports headless – Sergey Grinev Aug 20 '15 at 11:42
15

To avoid initialization Exception you have to either invoke Application.launch() method or simply instantiate a new JFXPanel() class (even if it isn’t used for anything). This will initiate JavaFxRuntime when application is started

To instantiate JFXPanel add below line in your code

 final JFXPanel fxPanel = new JFXPanel();

Import following package

import javafx.embed.swing.JFXPanel;
Sagar Damani
  • 862
  • 7
  • 12
5

There's also way to initialize toolkit explicitly, by calling: com.sun.javafx.application.PlatformImpl#startup(Runnable)

Little bit hacky, due to using *Impl, but is useful, if you don't want to use Application or JXFPanel for some reason.

krzychek
  • 300
  • 3
  • 9
3

see http://www.programcreek.com/java-api-examples/index.php?api=com.sun.javafx.application.PlatformImpl

com.sun.javafx.application.PlatformImpl.startup(()->{});
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186