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

查看:180
本文介绍了线程“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 示例(BrickBreaker、Ensemble、FXML-LoginDemo 和 SwingInterop)中的所有四个 JAR 文件.它们也都有额外"文件.

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:workspacesgaroup1outproductionjavafx1

希望这会有所帮助!

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

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