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

可紊 提交于 2019-12-14 03:06:42

问题


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!


回答1:


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




回答2:


An alternative path, if you prefer to not use JavaFX, would be to make use of the libraries that originated from JavaZOOM for the task of reading the mp3 files. I am seeing many offerings on github that have been derived from this source, for example, https://github.com/goxr3plus/java-sound-libraries But I have not made use of this particular library myself.

My preference has been to combine JavaFX for GUI with javax.audio.sampled, and a library I built that relies on java.sound.sampled.SourceDataLine for output. But I never bothered to implement reading mp3s. I tend to take the ogg/vorbis route when using compressed audio resources.



来源:https://stackoverflow.com/questions/56848305/java-lang-noclassdeffounderror-when-using-javafx-for-playing-sound-from-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!