【发布时间】:2019-10-01 04:11:51
【问题描述】:
我正在使用与 jdk11 配合使用的 Eclipse 2019-03。在javafx and Eclipse tutorial 之后,我已将 jdk11 添加到我的项目的模块路径中。我创建了一个包含所有 javafx jar 的 javafx11 用户库,并将其添加到我的模块路径中。 Reference to my post on Super User
我在 Ubuntu 18.04 的 Eclipse 中创建了这个非常简单的 JavaFX 程序:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("hellofx.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 300));
primaryStage.show();
}
}
没有错误并且可以编译,但是我得到了这个运行时错误:
Error: Could not find or load main class javac
Caused by: java.lang.ClassNotFoundException: javac
如何解决这个问题,以便我可以运行这个基本程序?
编辑:
java -version:
openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.04.1)
OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.04.1, mixed mode, sharing)
VM 参数,根据教程,我已在 Eclipse 中添加到项目的运行配置中:
--module-path /usr/lib/jvm/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml
有效的代码:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
Group root = new Group();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 300));
primaryStage.show();
}
}
Controller.java
package hellowFx2;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller {
@FXML
private Label label;
public void initialize() {
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
label.setText("Hello, JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + ".");
}
}
hellowFx.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hellofx.Controller">
<children>
<Label fx:id="label" text="Label" />
</children>
</StackPane>
项目结构:
hellowFx2
bin
src
hellowFx2(package)
Main.java
Controller.java
hellowfx.fxml
【问题讨论】:
-
您是否遵循本指南:openjfx.io/openjfx-docs/#IDE-Eclipse?
-
@JoséPereda 是的。我忘了提到,在发布这个问题之前,我已经将 VM 参数添加到我的运行配置中。
-
编辑您的问题并发布“如何”添加这些参数,不知何故顺序不正确,Eclipse 尝试将
javac作为您的主要课程 -
那么您已经安装了
openjfx,并且在您发布的路径下可以找到javafx-base.jar和其他JavaFX 模块及其本地库?添加 javafx11 库时,它看起来像 this?仍然无法解释您的错误。 -
@JoséPereda 是的。为了确认行为,我创建了另外两个 helloFx 项目。 helloFx2 在抱怨 fxml 库时表现稍有不同。 helloFx3 我摆脱了hellofx.fxml 和Controller.java 文件并使javafx 代码更加简单(仍然保持模块路径和vm 参数相同)并且应用程序工作正常。我会将有效的代码放在帖子中。
标签: java eclipse javafx classnotfoundexception ubuntu-18.04