中的JavaFX异常java

中的JavaFX异常java

本文介绍了线程“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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 17:10