首页 > 解决方案 > 客户端/服务器 jars - 线程“主”java.lang.reflect.InvocationTargetException 中的异常

问题描述

我在 Eclipse 中有一个 javafx 项目准备运行。我有一个客户端和一个服务器。我试图在 Eclipse 中运行服务器和客户端,一切正常,但是当我导出到可运行的 jars,一个到服务器,一个到客户端时,服务器 jar 工作正常,但是客户端抛出异常:线程“main”java 中的异常.lang.reflect.InvocationTargetException

尽管在 eclipse 中导出之前它工作正常。服务器和客户端都是以 GUI 开头的 javafx 应用程序。

ClientLauncher.java:

package client;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

public class ClientLauncher extends Application {

    public GCMClient gcmClient;

    private String host = "localhost";
    private int port = 5555;

    private double xOffset = 0;
    private double yOffset = 0;

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

    }

    @Override
    public void start(Stage PrimaryStage) throws Exception {

        System.out.println("Client Connection Established");
        Parent root = FXMLLoader.load(getClass().getResource("/sources/ServerLogin.fxml"));
        root.setOnMousePressed(new EventHandler<MouseEvent>() {

            public void handle(MouseEvent event) {
                xOffset = event.getSceneX();
                yOffset = event.getSceneY();
            }
        });
        root.setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                PrimaryStage.setX(event.getScreenX() - xOffset);
                PrimaryStage.setY(event.getScreenY() - yOffset);
            }
        });

        Scene scene = new Scene(root);
        PrimaryStage.setScene(scene);
        PrimaryStage.show();
    }

}

我得到的例外:

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml/javafx.fxml.FXMLLoader.load(Unknown Source)
        at client.ClientLauncher.start(ClientLauncher.java:33)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)

标签: javajavafxclient-server

解决方案


解决了:

我拼错了 ServerLogin.fxml

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

在我的例子中,它被命名为“ServerLogIn.fxml”。

在可运行的 jar 中,fxml 的名称似乎区分大小写,但在 eclipse 中却没有。这就是为什么它在 Eclipse 中运行良好而在 jar 中不起作用的原因。


推荐阅读