问题描述
我收到此错误
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:workspacesgaroup1outproductionjavafx1
希望这有帮助!
这篇关于线程“main"中的JavaFX异常java.lang.NoClassDefFoundError: javafx/application/Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!