线程&"main&"中的JavaFX异常;java.lang.NoClassDefFoundError:javafx/application/Application [英] JavaFX Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application

查看:401
本文介绍了线程&"main&"中的JavaFX异常;java.lang.NoClassDefFoundError:javafx/application/Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误

Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Ap
plication
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 13 more

当试图运行我的班级文件时,这就是源文件

When trying to run my class file, this is the source

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.concurrent.Executor;

public class TestApplication extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        new TestApplication();
    }

    public TestApplication() {
        try{
            final Parent root = FXMLLoader.load(Executor.class.getResource("test.fxml"));
            final Stage stage = new Stage(){{
                setScene(new Scene(root, 300, 250));
                setTitle("Test");
                setResizable(false);
                show();
            }};
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

fxml文件包含一个简单的gui.

The fxml file contains a simple gui.

推荐答案

在过去的几个小时中,我一直在处理同样的问题.即使我没有看到它写的明确,您似乎必须使用一种JavaFX打包工具,它既可以是Ant任务,也可以是javafxpackager可执行文件.(请参见 http://docs.oracle.com/javafx/2/deployment/packaging.htm,第5.3.1节).NetBeans IDE使用Ant来打包代码.(我正在使用IntelliJ)

I've worked on this very same issue for the past few hours. Even though I haven't seen it written explicitly, it appears that you MUST use one of the JavaFX packaging tools, which is either an Ant task or the javafxpackager executable. (See http://docs.oracle.com/javafx/2/deployment/packaging.htm, section 5.3.1). The NetBeans IDE uses Ant to package the code. (I'm using IntelliJ)

使用其中一种打包方法时,除了所有应用程序的代码和资源之外,它还会将以下内容添加到输出JAR文件中:

When you use one of those packaging methods, in addition to all of your application's code and resources, it also adds the following to your output JAR file:

/com/javafx/main/Main$1.class
/com/javafx/main/Main$2.class
/com/javafx/main/Main.class
/com/javafx/main/NoJavaFXFallback.class

使用这些工具后,您可以从命令行运行该应用程序:

With these in place, you can run the app from the command line:

java -jar outjar.jar

,一切正常.如果我删除了多余的com.javafx.main文件,则该应用程序将无法运行.

and all works fine. If I remove the extra com.javafx.main files, the app does not run.

要再次检查这一点,我查看了JavaFX示例中的所有四个JAR文件(BrickBreaker,Ensemble,FXML-LoginDemo和SwingInterop).它们也都有额外"文件.

To double-check this, I looked at all four JAR files in the JavaFX samples (BrickBreaker, Ensemble, FXML-LoginDemo, and SwingInterop). They all have the "extra" files, too.

对于我的小型测试应用程序,我使用以下命令行来构建可执行" JAR文件:

For my small test app, I used this command line to build an "executable" JAR file:

javafxpackager -createjar -appclass sample.Main -outfile outjar -v -nocss2bin -srcdir C:\workspaces\garoup1\out\production\javafx1

希望这会有所帮助!

这篇关于线程&"main&"中的JavaFX异常;java.lang.NoClassDefFoundError:javafx/application/Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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