简体   繁体   English

失败@应用程序启动方法(JavaFX 11、JDK 14 和 Intellij)

[英]Failure @ Application Start Method (JavaFX 11, JDK 14 & Intellij)

I'll try to keep this short but include as many details as I can, basically i'm trying to create a simple MPG calculator using Javafx but I've ran into a snag, whenever I attempt to run the main file some errors appear regarding the FXMLLoader but I cant for the life of me figure out why, I researched as many Q&A's as I could (and there is a lot of them) and I tried implementing the answers given but none of them worked.我会尽量保持简短,但包含尽可能多的细节,基本上我正在尝试使用 Javafx 创建一个简单的 MPG 计算器,但我遇到了一个障碍,每当我尝试运行主文件时,都会出现一些错误关于FXMLLoader,但我一生都无法弄清楚为什么,我尽可能多地研究了问答(并且有很多),我尝试实施给出的答案,但没有一个有效。

Im pretty sure this is the cause of my pain.我很确定这是我痛苦的原因。

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

I am pretty much a newbie when it comes to javafx so any assistance would be appreciated, if more information is required I will provide it.当谈到 javafx 时,我几乎是一个新手,所以如果需要更多信息,我将提供任何帮助,我们将不胜感激。

File Structure文件结构

File Structure文件结构

Main主要的

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样本.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>

Controller Controller

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.");
        }
    }
}

Error错误

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

As a quick side note, I was having issues setting up IntelliJ, for some reason when I imported JDK 14 for non-javafx programming it didnt import in any of the Jar files so I had to manually import those modules, same goes for JavaFX modules, here is the images of those as well, if something appears incorrect please let me know.作为一个快速的旁注,我在设置 IntelliJ 时遇到问题,由于某种原因,当我为非 javafx 编程导入 JDK 14 时,它没有导入任何 Jar 文件,所以我不得不手动导入这些模块,JavaFX 模块也是如此,这里也是这些图片,如果出现不正确的地方,请告诉我。

JDK1 JDK1

JDK2 JDK2

JDK3 JDK3

Edit: Just did a clean install of IntelliJ, JAvaFX,& JDK14, still got the same issue when I attempted to run the basic JavaFX "hello world" although the errors given are much more descriptive, Ill post the new errors here.编辑:刚刚完成了 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

Edit 2: Finally cleaned up the errors, after adding this line (see below) to VM options a single issue appeared.编辑 2:最后清理了错误,在将这一行(见下文)添加到 VM 选项后,出现了一个问题。 (Yes i put my path) (是的,我把我的路径)

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

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

Going to do some tinkering with the modules folder and see if i can fix this.打算对模块文件夹进行一些修补,看看我是否可以解决这个问题。

So after working on this for a couple of hours, i got a handle on the VM options (which was something I wasnt aware of when i first started) but i'm pretty sure the issue was caused by the use of forward and backward slashes, in a lot of the answers I read, they used forwards slashes instead of backwards slashes, I didnt realize this until about ten minutes ago, its a little embarrassing but, lesson learned.因此,在为此工作了几个小时后,我掌握了 VM 选项(这是我刚开始时不知道的事情),但我很确定问题是由使用正斜杠和反斜杠引起的,在我阅读的很多答案中,他们使用正斜杠而不是反斜杠,直到大约十分钟前我才意识到这一点,这有点尴尬,但是,吸取了教训。

Anyways, if youre still having this issue i'll give a basic rundown on what should be done (although there are a ton of good answers out there, just remember to use backslashes, dont be a dumb dumb like me).无论如何,如果你仍然有这个问题,我会给出一个关于应该做什么的基本纲要(虽然那里有很多好的答案,但请记住使用反斜杠,不要像我一样愚蠢)。

Important Note: This implementation is for Windows Systems重要提示:此实现适用于 Windows 系统

1st: Ensure that you have your JDK and JavaFX downloaded, try to keep the folder names simple (like JDK14 or JavaFX14).第一:确保您已下载 JDK 和 JavaFX,尽量保持文件夹名称简单(如 JDK14 或 JavaFX14)。

2nd: Open a new project and select your "Project SDK", in our case select the JDK you downloaded.第二:打开一个新项目和 select 你的“项目 SDK”,在我们的例子中 select 你下载的 JDK。

3rd: After creating the project go to File -> Project Structure, then under 'Project Settings' select Libraries, then click the Plus icon to the right of project settings, select Java then navigate to your JavaFX folder and select 'lib'. 3rd: After creating the project go to File -> Project Structure, then under 'Project Settings' select Libraries, then click the Plus icon to the right of project settings, select Java then navigate to your JavaFX folder and select 'lib'.

4th: If you run the project now, you will get a runtime error, instead go to Run (at the top) then select 'Edit Configurations'.第四:如果你现在运行项目,你会得到一个运行时错误,而不是 go 运行(在顶部)然后 select '编辑配置'。 In the 'VM Options:' text field enter:在“VM 选项:”文本字段中输入:

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

And now you're done, welcome to JavaFX.现在你已经完成了,欢迎来到 JavaFX。

TL;DR: Forward slashes are for URI's and backslashes are for local (windows) files, do not forget this or end up in the same situation as me, going insane from doing the same thing over and over again expecting a different result. TL;DR:正斜杠用于 URI,反斜杠用于本地(Windows)文件,不要忘记这一点,否则会遇到和我一样的情况,因为一遍又一遍地做同样的事情而期待不同的结果而发疯。

Have a good day.祝你有美好的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM