错误:无法找到或加载主类应用程序。主因:java.lang.NoClassDefFoundError:javafx / application / Application JDK 11

问题描述 投票:3回答:2

我陷入一个非常基本的问题。我使用JavaFX创建了一个简单的hello world程序,该程序在JDK 1.8上运行良好。但是,当我切换到JDK-11时,它将引发以下异常:

Error: Could not find or load main class application.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

以下是我在Eclipse中编写的代码。

package application;

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


public class Main extends Application {

    private Scene theScene;
    @Override
    public void start(Stage primaryStage) {
try {

            FXMLLoader loader = new FXMLLoader(getClass().getResource("MyScene.fxml"));
            Parent mainPane = loader.load();


            theScene = new Scene(mainPane);
            primaryStage.setScene(theScene);
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void setTheScene(Scene theScene) {
        this.theScene = theScene;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
java javafx java-11
2个回答
1
投票

jdk 11没有Javafx支持。 Oracle删除了它。但是您可以使用Maven将javafx添加到您的项目中。


1
投票

在从拉伸升级到失败之后,我在debian上遇到了同样的问题,但是现在,一切都很好:

java --version

openjdk 11.0.4 2019-07-16

要使用终端运行Java-fx应用程序,请遵循以下步骤:

  1. 安装openjfx(如果尚未安装):sudo apt install openjfx

  2. 列出javafx库的位置:dpkg-query -L openjfx输出应如下所示:

。在/ usr在/ usr /股的/ usr /共享/ DOC的/ usr /共享/ DOC /的OpenJFX/usr/share/doc/openjfx/TODO.Debian/usr/share/doc/openjfx/changelog.Debian.gz在/ usr /共享/ DOC /的OpenJFX /版权的/ usr /共享/的OpenJFX的/ usr /共享/的OpenJFX / lib中/usr/share/openjfx/lib/javafx.properties/usr/share/openjfx/lib/javafx.base.jar/usr/share/openjfx/lib/javafx.controls.jar/usr/share/openjfx/lib/javafx.fxml.jar/usr/share/openjfx/lib/javafx.graphics.jar/usr/share/openjfx/lib/javafx.media.jar/usr/share/openjfx/lib/javafx.swing.jar/usr/share/openjfx/lib/javafx.web.jar

  1. 运行jar应用程序通过包含javafx路径和模块java --module-path $PATH_TO_OPENJFX-LIB --add-modules module_1,module_2,module_3,...,module_n -jar $PATH_TO_JAR_FILE

实施例:

java --module-path /usr/share/openjfx/lib --add-modules=javafx.controls,javafx.fxml,javafx.base,javafx.media,javafx.web,javafx.swing -jar '/home/lotfi/Documents/MyAppfolder/my_application.jar'
© www.soinside.com 2019 - 2024. All rights reserved.