1

I'm getting the following error when trying to play a MediaPlayer sound with the jar file created. There is absolutely no problem playing sounds if I run the program directly in VSCode and the jar also worked properly on my previous non M1 iMac Pro which was running High Sierra. I'm using Maven and my Mac uses Monterey.

Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: javafx/scene/media/Media
    at com.parler.utils.Prononce.playSound(Prononce.java:94)
    at com.parler.TextBubble.lambda$1(TextBubble.java:76)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3566)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3870)
    at javafx.scene.Scene.processMouseEvent(Scene.java:1854)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2587)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:413)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at com.sun.glass.ui.View.notifyMouse(View.java:942)
    at com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
Caused by: java.lang.ClassNotFoundException: javafx.scene.media.Media
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 34 more

VSCode terminal:

% java -version
java version "17.0.3.1" 2022-04-22 LTS

Mac terminal:

java -version
java version "17.0.3.1" 2022-04-22 LTS
Hasen
  • 11,710
  • 23
  • 77
  • 135
  • Javafx is no longer bundled with the JVM and you probably used an old one on your old machine. – Thorbjørn Ravn Andersen Jul 01 '22 at 15:39
  • @Thorbjørn Ravn Andersen So the JVM is not required to launch a JAR? – Hasen Jul 01 '22 at 15:40
  • That's not what they said. Back in Java 11, the JavaFX library was _removed_ from the JDK that is distributed by Oracle. It is now a separate library, and needs to be included with your application similar to any other third-party library. – Slaw Jul 01 '22 at 15:41
  • @Slaw But Javafx with a Jar works without it, is that what you're saying? – Hasen Jul 01 '22 at 15:42
  • Do you use the same JVM in vs code as on the command line? – Thorbjørn Ravn Andersen Jul 01 '22 at 15:45
  • vscode terminal:`java -version` `java version "17.0.3.1" 2022-04-22 LTS` and in mac Mac command line `java version "17.0.3.1" 2022-04-22 LTS` – Hasen Jul 01 '22 at 15:47
  • What does `java -version` output on your development machine? What does it output on the machine where you get the above error? – Slaw Jul 01 '22 at 15:47
  • I ran `java -version` in the vscode terminal and then in my Mac terminal. Those are the outputs above. – Hasen Jul 01 '22 at 15:48
  • Added into my question for clarity. – Hasen Jul 01 '22 at 15:50
  • @Slaw I'm running everything on the same machine currently. I no longer have the old iMac Pro running Mojave that I mentioned. But all the Jars worked no problem as they were. – Hasen Jul 01 '22 at 15:51
  • @Slaw To be clear I only have one Mac right now and it all works fine running directly in vscode but in a Jar the mediaplayer throws the error in the description. The Jar is built with Maven Shade inside of VSCode. The Jar works fine, the only problem is when playing a sound. I'm not using `--module path` I'm just using Maven. – Hasen Jul 01 '22 at 15:52
  • Okay. Since you mention Maven, am I correct in assuming you have JavaFX declared as a dependency in your `pom.xml` file? – Slaw Jul 01 '22 at 15:55
  • @Slaw Yes it wouldn't work at all without that in the pom.xml file. `org.openjfxjavafx-controls17-ea+7` – Hasen Jul 01 '22 at 15:56
  • Do you also declare a dependency on the `javafx-media` artifact? – Slaw Jul 01 '22 at 15:58
  • @slaw No I don't have that dependency but it works fine in VSCode without it and no errors are displayed. – Hasen Jul 01 '22 at 15:58
  • 2
    Declare that dependency and try building and running the JAR again. Does it work? – Slaw Jul 01 '22 at 15:59
  • @slaw Yes that works. Why is it the Jar requires that but vscode doesn't? – Hasen Jul 01 '22 at 16:02
  • 2
    I honestly don't know. Perhaps you have a JavaFX SDK installed and there's a configuration mismatch between Maven and VS Code. I'd likely need to actually _see_ your entire development environment to give a definitive answer, which can't happen here. At least you got the JAR to work. However, you may want to consider looking into deploying your JavaFX application via `jlink` + `jpackage`. Check out the "Packaging" section in the [JavaFX tag info page](https://stackoverflow.com/tags/javafx/info) for more information. – Slaw Jul 01 '22 at 16:31
  • @slaw Yeah it's very odd, as we can see I have the same java version on both, doesn't seem to make much sense. That's why I couldn't understand why it worked but didn't work as a jar. Anyway, thanks for your help. – Hasen Jul 01 '22 at 16:56
  • Your error is around the Media class which is in javafx-media, not javafx-controls. Mac M1 support was only implemented relatively recently, and may not be available or stable in an early access distribution such as 17-ea+7. You could try JavaFX 18.0.1 with JDK 18.0.1. If you want a version of the JDK packaged for M1 which includes JavaFX, then you can use Liberica full JDK or [Zulu JDK FX](https://stackoverflow.com/a/72793112/1155209). – jewelsea Jul 01 '22 at 17:22

0 Answers0