问题描述
我陷入一个非常基本的问题.我使用JavaFX创建了一个简单的hello world程序,该程序在JDK 1.8上运行良好.但是,当我切换到JDK-11时,它将引发以下异常:
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:
Error: Could not find or load main class application.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
以下是我在eclipse中编写的代码.
Following is the code I wrote in 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);
}
}
推荐答案
从Stretch升级到Buster后,我在debian上也遇到了同样的问题,但是现在,一切都很好:
I faced the same issue on debian after upgrading from stretch to buster, but now, everything is fine:
java --version
要使用终端运行Java-fx应用程序,请按照以下步骤操作:
To run a java-fx application using terminal, follow these steps:
-
安装openjfx(如果尚未安装):
sudo apt install openjfx
Install openjfx (if it is not already installed):
sudo apt install openjfx
列出javafx库的位置:dpkg-query -L openjfx
输出应该是这样的:
List the javafx library location: dpkg-query -L openjfx
The output should be like this:
- 通过包含javafx路径和模块来运行jar应用程序 :
java --module-path $PATH_TO_OPENJFX-LIB --add-modules module_1,module_2,module_3,...,module_n -jar $PATH_TO_JAR_FILE
- 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
示例:
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'
这篇关于错误:找不到或加载主类应用程序.主因:java.lang.NoClassDefFoundError:javafx/application/Application JDK 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!