【问题标题】:Client/Server jars - Exception in thread "main" java.lang.reflect.InvocationTargetException客户端/服务器 jars - 线程“主”java.lang.reflect.InvocationTargetException 中的异常
【发布时间】:2019-05-15 21:14:11
【问题描述】:

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

虽然它在 eclipse 中导出之前工作正常。 Server 和 Client 都是以 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)

【问题讨论】:

  • 编辑您的问题,并向我们展示 ClientLauncher.java 的第 33 行。更好的是 ClientLauncher.java 的第 23-33 行,因此我们可以看到您如何创建要传递给 FXMLLoader.load 的位置。
  • 添加了 ClientLauncher.java @VGR
  • 您的客户端 jar 是否包含 ServerLogin.fxml 条目?条目是否位于 jar 内 sources/ServerLogin.fxml
  • 是的,它应该包含,我创建 jar 的方式是单击导出,然后单击 jar 并选择主类。客户端的主类是ClientLauncher,它加载sources/ServerLogin.fxml
  • 您是否检查并验证了客户端 jar 的内容?

标签: java javafx client-server


【解决方案1】:

已解决:

我在

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2014-03-04
    • 2011-04-28
    相关资源
    最近更新 更多