错误:无法找到或加载主类 application.Main 导致:java.lang.NoClassDefFoundError:javafx/application/Application JDK 11

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53319350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:46:54  来源:igfitidea点击:

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

javajavafxjava-11

提问by Shahrukh S.

I am stuck at a very basic problem. I have created a simple hello world program using JavaFX which works fine on JDK 1.8. But when I switch to JDK-11 it throws following exception:

我被困在一个非常基本的问题上。我使用 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

Following is the code I wrote in eclipse.

以下是我在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);
    }
}

回答by Alexandr Tkach

jdk 11 does not have javafx support. Oracle removed it. But you can add javafx to your project using Maven.

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

回答by Lotfi Hocini

I faced the same issue on debian after upgrading from stretch to buster, but now, everything is fine:

从stretch升级到buster后,我在debian上遇到了同样的问题,但现在一切都很好:

java --version

openjdk 11.0.4 2019-07-16

openjdk 11.0.4 2019-07-16

To run a java-fx application using terminal, follow these steps:

要使用终端运行 java-fx 应用程序,请按照下列步骤操作:

  1. Install openjfx (if it is not already installed):
    sudo apt install openjfx

  2. List the javafx library location:
    dpkg-query -L openjfx
    The output should be like this:

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

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

. /usr /usr/share /usr/share/doc /usr/share/doc/openjfx /usr/share/doc/openjfx/TODO.Debian /usr/share/doc/openjfx/changelog.Debian.gz /usr/share/doc/openjfx/copyright /usr/share/openjfx /usr/share/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

. /usr /usr/share /usr/share/doc /usr/share/doc/openjfx /usr/share/doc/openjfx/TODO.Debian /usr/share/doc/openjfx/changelog.Debian.gz /usr/share /doc/openjfx/copyright /usr/share/openjfx /usr/share/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. Run the jar application by including the javafx path and modules:
    java --module-path $PATH_TO_OPENJFX-LIB --add-modules module_1,module_2,module_3,...,module_n -jar $PATH_TO_JAR_FILE
  1. 通过包含 javafx 路径和模块来运行 jar 应用程序:
    java --module-path $PATH_TO_OPENJFX-LIB --add-modules module_1,module_2,module_3,...,module_n -jar $PATH_TO_JAR_FILE

Example:

例子:

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'

回答by Nguy?n Tùng Lam

In Eclipse : run configurations -> arguments -> VM arguments :

在 Eclipse 中:运行配置 -> 参数 -> VM 参数:

--module-path ${project_classpath:REPLACE_ME_WITH_YOUR_PROJECT_NAME} 

--add-modules javafx.controls,javafx.fxml