【问题标题】:Failure @ Application Start Method (JavaFX 11, JDK 14 & Intellij)失败@应用程序启动方法(JavaFX 11、JDK 14 和 Intellij)
【发布时间】:2020-07-21 21:59:23
【问题描述】:

我会尽量保持简短,但包含尽可能多的细节,基本上我正在尝试使用 Javafx 创建一个简单的 MPG 计算器,但每当我尝试运行主文件时我遇到了障碍FXMLLoader 出现了一些错误,但我一生都无法弄清楚为什么,我尽可能多地研究了问答(并且有很多),我尝试实施给出的答案,但没有一个有效。

我很确定这是我痛苦的原因。

父根 = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));

在 javafx 方面,我几乎是一个新手,所以如果需要更多信息,我将提供任何帮助。

文件结构

File Structure

主要

package sample;

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 {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/sample/sample.fxml"));
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

sample.FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>


<GridPane alignment="center" hgap="10" prefHeight="267.0" prefWidth="251.0" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <AnchorPane prefHeight="279.0" prefWidth="251.0">
         <children>
            <Label layoutX="12.0" layoutY="14.0" prefHeight="35.0" prefWidth="233.0" text="Miles Per Gallon Calculator" textAlignment="CENTER" underline="true">
               <font>
                  <Font size="19.0" />
               </font>
            </Label>
            <Label layoutX="16.0" layoutY="69.0" prefHeight="27.0" prefWidth="50.0" text="Miles">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <Label layoutX="16.0" layoutY="113.0" prefHeight="27.0" prefWidth="68.0" text="Gallons">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <Button layoutX="27.0" layoutY="151.0" mnemonicParsing="false" onAction="#calculateMPG" prefHeight="42.0" prefWidth="206.0" text="Calculate MPG" />
            <TextField fx:id="milesField" layoutX="84.0" layoutY="70.0" />
            <TextField fx:id="gallonsField" layoutX="84.0" layoutY="114.0" />
            <Label layoutX="16.0" layoutY="204.0" prefHeight="27.0" prefWidth="68.0" text="MPG">
               <font>
                  <Font size="18.0" />
               </font>
            </Label>
            <TextField fx:id="mpgField" layoutX="84.0" layoutY="205.0" />
         </children>
      </AnchorPane>
   </children>
</GridPane>

控制器

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import java.text.DecimalFormat;

public class Controller {
    DecimalFormat df = new DecimalFormat("#.###"); double mpg;

    @FXML
    private TextField milesField;

    @FXML
    private TextField gallonsField;

    @FXML
    private TextField mpgField;

    @FXML
    void calculateMPG(ActionEvent event) {
        try {
            double miles = Double.parseDouble(milesField.getText());
            double gallons = Double.parseDouble(gallonsField.getText());
            if(gallons == 0){
                mpgField.setText("Cannot Divide by zero");
            }
            else {
                mpg = miles / gallons;
                mpgField.setText(df.format(mpg));
            }

        } catch (NumberFormatException e){
            mpgField.setText("Please enter real numbers.");
        }
    }
}

错误

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NoSuchMethodError: 'java.lang.Object sun.reflect.misc.ReflectUtil.newInstance(java.lang.Class)'
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application sample.Main

Process finished with exit code 1

顺便说一下,我在设置 IntelliJ 时遇到了问题,由于某种原因,当我为非 javafx 编程导入 JDK 14 时,它没有导入任何 Jar 文件,所以我不得不手动导入这些模块,同样如此对于 JavaFX 模块,这也是它们的图像,如果出现不正确的地方,请告诉我。

JDK1

JDK2

JDK3

编辑: 刚刚完成了 IntelliJ、JAvaFX 和 JDK14 的全新安装,当我尝试运行基本的 JavaFX“hello world”时仍然遇到同样的问题,尽管给出的错误更具描述性,我将在此处发布新错误。

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6af6aa0c) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x6af6aa0c
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at sample.Main.start(Main.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application sample.Main

Process finished with exit code 1

编辑 2: 最后清理了错误,在将这一行(见下文)添加到 VM 选项后,出现了一个问题。 (是的,我把我的路径)

-p /%EnterPathToJavaFX%/lib --add-modules javafx.controls

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.base not found

打算对模块文件夹进行一些修补,看看我是否可以解决这个问题。

【问题讨论】:

    标签: java intellij-idea javafx compiler-errors


    【解决方案1】:

    因此,在研究了几个小时之后,我掌握了 VM 选项(这是我刚开始时不知道的),但我很确定问题是由使用 forward 引起的和反斜杠,在我阅读的很多答案中,他们使用正斜杠而不是反斜杠,直到大约十分钟前我才意识到这一点,这有点尴尬,但是,吸取教训。

    无论如何,如果您仍然遇到此问题,我将简要说明应该做什么(尽管那里有很多好的答案,但请记住使用反斜杠,不要像我一样愚蠢)。

    重要提示:此实现适用于 Windows 系统

    第一: 确保您已下载 JDK 和 JavaFX,尽量保持文件夹名称简单(如 JDK14 或 JavaFX14)。

    第二次: 打开一个新项目并选择您的“Project SDK”,在我们的例子中选择您下载的 JDK。

    第三次: 创建项目后,转到文件 -> 项目结构,然后在“项目设置”下选择“库”,然后单击项目设置右侧的加号图标,选择 Java,然后导航到您的 JavaFX 文件夹并选择“lib”。

    第四名: 如果您现在运行该项目,您将收到运行时错误,而是转到运行(在顶部),然后选择“编辑配置”。在“VM 选项:”文本字段中输入:

    -p *Your_Drive_Partition*:\*path_to_JFX*\JavaFX\lib --add-modules=javafx.controls,javafx.fxml
    

    现在你已经完成了,欢迎使用 JavaFX。

    TL;DR: 正斜杠用于 URI,反斜杠用于本地(Windows)文件,不要忘记这一点,否则会遇到和我一样的情况,因为做同样的事情而发疯一遍又一遍地期待不同的结果。

    祝你有美好的一天。

    【讨论】:

    • 也许你应该指出,你所说的一切只对Windows有效。不要在 UNIX 上使用反斜杠。
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 2019-04-26
    • 1970-01-01
    • 2018-11-22
    • 2019-09-02
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多