Search code examples
javajavafxaudiocompilationrunnable-jar

java.lang.NoClassDefFoundError when using JavaFX for playing sound from jar


I want to be able to play sound from mp3 files for which I saw posts recommending the usage of JavaFX. I implemented the MediaPlayer and initialized the JFXPanel and in eclipse, everything works lovely.

Yet when I export to a runnable jar, and try running the program, I get the following error message: java.lang.NoClassDefFoundError: javafx/scene/media/MediaException.

I presume this is from the exclusion of JavaFX in the newer JRE versions (which I came across during my search to a solution). My main question is how do I ship the jar with JavaFX? Do I have to include a jar, and if yes, where do I get it? Because eclipse doesn't seem to package JavaFX into my runnable if I'm not mistaken.

Here an example which, for me, already triggers this behavior:

// This would throw a java.lang.NoClassDefFoundError for the JFXPanel but is effectively the same problem
public class Test extends Application
{
    public static void main(String[] Args)
    {
        launch(Args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        StackPane root = new StackPane();

        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

Thank you for your help!


Solution

  • JavaFx was removed into JDK> = 11 and now is a separate project opensurse [openjfx] (https://openjfx.io/). And now it is to be made more difficult to create a version of the application javafx runnable everywhere, but it is a continuous evolution and I think that this is good documentation [doc-image-live] (https://openjfx.io/openjfx-docs/#modular).

    I had a problem simile when I used for the developing the JDK 1.8 but in my version java system is openjdk11, I think this is the same case.

    Your example is wrong because not is a JavaFX application. The JavaFX application must extend the javafx.application.Application and in the main call the method launch, this method will call the method start inherited from Application.

    This is a simple example of the Oracle

    public class HelloWorld extends Application {
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("Hello World!");
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
    
                @Override
                public void handle(ActionEvent event) {
                    System.out.println("Hello World!");
                }
            });
    
            StackPane root = new StackPane();
            root.getChildren().add(btn);
            primaryStage.setScene(new Scene(root, 300, 250));
            primaryStage.show();
        }
    }
    

    ps: When you speak the javafx, you must add the java version because we don't know your java version