java - 客户端/服务器 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)
解决方案
解决了:
我拼错了 ServerLogin.fxml
Parent root = FXMLLoader.load(getClass().getResource("/sources/ServerLogin.fxml"));
在我的例子中,它被命名为“ServerLogIn.fxml”。
在可运行的 jar 中,fxml 的名称似乎区分大小写,但在 eclipse 中却没有。这就是为什么它在 Eclipse 中运行良好而在 jar 中不起作用的原因。
推荐阅读
- python - 使用 Flask 对 Pandas 中的给定列数据执行算术运算
- ember.js - 为什么我的 Octane 组件方法中出现“这是未定义的”?
- c - 程序查找总和,如果输入“y”,则使用 do while 循环重复该过程
- html - 是否可以使用css绘制具有不同长度的垂直线?
- c - C:从输入的数字生成结构变量名称
- php - PHP ZMQ 不能在 Windows 上运行,但可以在 Linux 上运行?
- amazon-web-services - IoT 的 localstack 错误:在我们收到有效响应之前连接已关闭
- javascript - 仅在所有图像加载后才让 HTML 继续
- ios - uidatepicker 总是滚动到当前时间
- c# - C# 接口:从具体类中提取额外的公共属性值(不是接口的一部分)