【问题标题】:javafx Run time failurejavafx 运行时失败
【发布时间】:2019-08-02 18:10:11
【问题描述】:

大家好,我一直在 NetBeans 8.2 和 Gluon Scene Builder 上开发 JavaFx 应用程序。最初程序给了我一个输出。开发后我收到错误。

(我在这个平台上尝试了很多建议,我清理并构建了项目,我更改了我的 fxml 文件位置,我删除并重新上传了 Scene Builder,我什至删除了整个项目并再次写下来看看我是否'我忘记了一些东西。该程序不包含任何语法错误。我的服务器和客户端都在工作。运行JavaFXMLApplication.java时发生错误)

我可能遗漏了什么,没有错。

HP 的用户变量

PATH:   C:\Users\HP\Envs\gui; C:\Users\HP\Desktop\jdk-11.0.2

系统变量

PATH:C:\Program Files\Python37\Scripts\;C:\Program 
Files\Python37\;C:\Users\HP\Envs\gui;C:\Program 
Files\Java\jdk1.8.0_111\bin;C:\Program Files\Java\jdk-11.0.2\bin

我的 fxml 文件

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="javafxmlapplication.FXMLDocumentController">

代码:

package javafxmlapplication;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class JavaFXMLApplication extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource
    ("/src/javafxmlapplication/fxml/FXMLDocument.fxml"));

        Scene scene = new Scene(root,900,600);

        stage.setTitle("GUI");
        stage.setScene(scene);
        stage.show();

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask(){
            @Override
            public void run(){
                Random rand = new Random();
                int myrand = rand.nextInt(15)+1;
                System.out.println((myrand+1)+"blue");
                root.getChildrenUnmodifiable().get(myrand).setStyle("-fx-background-color: #001f3f");
                    try{
                        Thread.sleep(1000);
                    }
                    catch(InterruptedException e){
                        //e.printStackTrace();
                        System.out.println(e);
                    }
                root.getChildrenUnmodifiable().get(myrand).setStyle("-fx-background-color : #FF4136");
            }
        },0,1000);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

在错误消息系统中标记了这一行,但在行号上没有给出错误(我尝试添加 getClassLoader() 它给了我同样的错误)

    Parent root = FXMLLoader.load(getClass().getResource
    ("/src/javafxmlapplication/fxml/FXMLDocument.fxml"));

我的系统在 cmd 中有 java 和 javac 代码的正输出。

Executing C:\Users\HP\Documents\NetBeansProjects\JavaFXMLApplication\dist\run1672502609\JavaFXMLApplication.jar using platform C:\Program Files\Java\jdk1.8.0_111\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    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 com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Exception running application javafxmlapplication.JavaFXMLApplication
Java Result: 1
Deleting directory C:\Users\HP\Documents\NetBeansProjects\JavaFXMLApplication\dist\run1672502609
jfxsa-run:
BUILD SUCCESSFUL (total time: 2 seconds)

下面这个错误

javafxmlapplication.JavaFXMLApplication.start(JavaFXMLApplication.java:25)

点这个

Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("/src/javafxmlapplication/fxml/FXMLDocument.fxml"));

任何帮助将不胜感激我在循环中。

【问题讨论】:

  • a) 不要从后台线程修改 GUI。如果您想实现从TimerTask 更新gui,请使用Platform.runLater(请务必从runnable 中排除像Thread.sleep 这样的长时间运行的操作;您可能需要使用Platform.runLater 两次)。 Parent root = FXMLLoader.load(getClass().getResource("/javafxmlapplication/fxml/FXMLDocument.fxml")); 应该可以工作,除非 Netbeans 不包含该资源或者我错过了一个错字;如果没有,请使用 Netbeans 的文件浏览器查看生成的 jar 并检查资源 (\dist\run***\JavaFXMLApplication.jar)
  • 没有run*** 文件人只有:web-files 文件、.html、jar.jnpl
  • 它在您发布的错误上方显示Executing C:\Users\HP\Documents\NetBeansProjects\JavaFXMLApplication\dist\run1672502609\JavaFXMLApplication.jar using ...。罐子应该在附近的某个地方......
  • 是的,有一个 jar 文件,但我不知道这个文件是什么。你能澄清一下吗?
  • 在我使用的 Netbeans 版本中,您可以打开 .jar 文件并使用 Projects 旁边的 Files 选项卡查看其内容。您可以使用此功能来验证 fxml 是否在您想要的位置或找出它的放置位置。 (请记住,您在那里看到的.s 对应于资源路径中的/s,您需要在路径前面添加/ 以使getResource 在根目录中开始查找。) .通过这种方式,您可以验证这不仅仅是项目设置不包含 fxml 文件作为资源的问题。 (不过先检查不同的路径。)

标签: java javafx


【解决方案1】:

您使用了错误的路径来加载您的 fxml 文件。使用fxml/FXMLDocument.fxml/javafxapplication/fxml/FXMLDocument.fxml

查找具有给定名称的资源。搜索与给定类关联的资源的规则由该类的定义类加载器实现。这个方法委托给这个对象的类加载器。如果此对象由引导类加载器加载,则该方法委托给 ClassLoader.getSystemResource(java.lang.String)。 在委托之前,使用此算法从给定的资源名称构造一个绝对资源名称:

  • 如果名称以“/”开头(“\u002f”),则资源的绝对名称是名称中“/”后面的部分。
  • 否则,绝对名称的格式如下:modified_pa​​ckage_name/name

来自javadoc

所以以下应该可以工作:

Parent root = FXMLLoader.load(getClass().getResource("fxml/FXMLDocument.fxml"));

或者:

Parent root = FXMLLoader.load(getClass().getResource("/javafxapplication/fxml/FXMLDocument.fxml"));

【讨论】:

  • 向兄弟寻求建议,但我也尝试过fxml/FXMLDocument.fxml parth 是由系统自动创建的,因此它也不起作用,因此我更改了路径。当我编译时没有getClassLoader(). 或从系统创建的路径fxml/FXMLDocument.fxml 给了我通知
  • Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing. Please set manifest.custom.codebase property to override the current default non-secure value '*'. Launching &lt;fx:deploy&gt; task from C:\Program Files\Java\jdk1.8.0_111\jre\..\lib\ant-javafx.jar No base JDK. Package will use system JRE. No base JDK. Package will use system JRE.
猜你喜欢
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 2021-06-03
  • 1970-01-01
相关资源
最近更新 更多