使用JavaFX从jar播放声音时出现java.lang.NoClassDefFoundError [英] java.lang.NoClassDefFoundError when using JavaFX for playing sound from jar

查看:270
本文介绍了使用JavaFX从jar播放声音时出现java.lang.NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够播放mp3文件中的声音,为此我看到了一些推荐使用JavaFX的帖子.我实现了MediaPlayer并初始化了JFXPanel,并且在eclipse中,一切正常.

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.

但是,当我导出到可运行的jar并尝试运行该程序时,出现以下错误消息:java.lang.NoClassDefFoundError:javafx/scene/media/MediaException.

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.

我认为这是由于JavaFX不包含在较新的JRE版本中(我在搜索解决方案时遇到的).我的主要问题是如何使用JavaFX运送罐子?我必须包括一个罐子吗?如果是,我从哪里得到它?因为如果我没记错的话,eclipse似乎并没有将JavaFX打包到我的runnable中.

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();
    }
}

谢谢您的帮助!

推荐答案

JavaFx已被移至JDK> = 11,现在是一个单独的项目opensurse [openjfx]( https://openjfx.io/). 现在要在任何地方创建可运行的应用程序javafx版本变得更加困难,但是它是不断发展的,我认为这是很好的文档[doc-image-live](

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).

当我用于开发JDK 1.8时,我遇到了一个类似问题,但是在我的Java系统版本中,openjdk11是我的情况,

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.

您的示例是错误的,因为它不是JavaFX应用程序. JavaFX应用程序必须扩展 javafx.application.Application ,并且在主要调用方法 launch 中,该方法将调用从继承的方法 start 申请.

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.

这是Oracle的简单示例

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:在讲javafx时,必须添加Java版本,因为我们不知道您的Java版本

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

这篇关于使用JavaFX从jar播放声音时出现java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆