代码工作正常,然后在安装 maven 后,jar 和 IDE 中都有错误
Code works fine, then after maven install, there are errors in both the jar and the IDE
应用程序启动方法异常:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$launchApplication4(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
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 edu.citytech.view.EmployeeMain.start(EmployeeMain.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(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$null7(WinApplication.java:177)
... 1 more
Exception running application edu.citytech.view.EmployeeMain
这是 class 的主要方法,EmployeeMain.java:
package edu.citytech.view;
import edu.citytech.utitlity.GlobalVariables;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class EmployeeMain extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("EmployeeView.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
// * new *
primaryStage.setResizable(false);
primaryStage.setTitle("Employee List");
primaryStage.getIcons().add(new Image(EmployeeMain.class.getResourceAsStream("People.jpg")));
}
public static void main(String[] args) {
if (args.length > 0) {
GlobalVariables.portNumber = (Integer.parseInt(args[0]));
}
launch(args);
}
}
解决此问题的方法是向任何文件添加 space 并保存它,然后重新启动 Eclipse IDE,然后 运行 从 [=24] 中重新启动它=].当我 运行 mvn clean 和 mvn install,然后 运行ning 它创建的 jar 文件时,它会崩溃。我的 class 里的每个人都遇到了这个问题,老师不知道如何解决。它只对某些人有效,对其他人无效。但是这一次,无论我尝试什么,这都不起作用。在我使用 maven 创建 jar 之前,只能在 IDE 中工作。然后就不行了,直到我修改保存一个文件,然后重启IDE。一切都被正确命名,它只是正常工作,而不是随机工作。这是 IDE 问题还是项目问题?
我该怎么做才能制作一个在 运行 时可用的 jar 文件?
可能有 3 个问题
- 听起来您需要 IDE 重新编译代码才能在 eclipse 中再次运行?不确定为什么会这样,除非它使用 java 版本 ...
eclipse 通常会根据任何更改重建(例如,您的 space 添加到文件中)
你的eclipseIDE和maven compile使用的是同一个java版本吗?
删除您的 ./target 目录并
将此添加到 Maven pom.xml 以确保您使用的是正确的 java 版本(例如 java 1.8):
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
- 在 jar 构建中包含文件
另外,您的 EmployeeView.fxml 副本应该在 src/main/resources 中,除非您包括:
,否则该文件不会进入您的 jar 文件
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
- 该代码在运行时无法在 jar 文件中运行,请参阅以下答案:
How to reference a resource file correctly for JAR and Debugging?
并更新您的代码以使用 getREsourceAsStream,例如这是我使用的方法:
public void load(String resource, HashSet<String> hashset) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is_awl = classloader.getResourceAsStream(resource);
BufferedReader reader = new BufferedReader(new InputStreamReader(is_awl));
String line;
try {
while ((line = reader.readLine()) != null) {
hashset.add(line.trim());
}
} catch (IOException e) {
System.err.println("LexicalFeatures can't initialise:"
+ resource + " hashset: "+ e.getMessage());
e.printStackTrace();
}
}
我想出了如何让它发挥作用。真的很烦人但真的很简单。我将在这里写给可能通过搜索引擎找到此页面的任何人。
右键单击项目文件夹 > Maven > 更新项目...
更新所选项目
在本地终端
右击项目文件夹和运行
mvn 安装
并且不要进行 MVN 清理
应用程序启动方法异常:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$launchApplication4(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
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 edu.citytech.view.EmployeeMain.start(EmployeeMain.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater3(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$null7(WinApplication.java:177)
... 1 more
Exception running application edu.citytech.view.EmployeeMain
这是 class 的主要方法,EmployeeMain.java:
package edu.citytech.view;
import edu.citytech.utitlity.GlobalVariables;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
public class EmployeeMain extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("EmployeeView.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
// * new *
primaryStage.setResizable(false);
primaryStage.setTitle("Employee List");
primaryStage.getIcons().add(new Image(EmployeeMain.class.getResourceAsStream("People.jpg")));
}
public static void main(String[] args) {
if (args.length > 0) {
GlobalVariables.portNumber = (Integer.parseInt(args[0]));
}
launch(args);
}
}
解决此问题的方法是向任何文件添加 space 并保存它,然后重新启动 Eclipse IDE,然后 运行 从 [=24] 中重新启动它=].当我 运行 mvn clean 和 mvn install,然后 运行ning 它创建的 jar 文件时,它会崩溃。我的 class 里的每个人都遇到了这个问题,老师不知道如何解决。它只对某些人有效,对其他人无效。但是这一次,无论我尝试什么,这都不起作用。在我使用 maven 创建 jar 之前,只能在 IDE 中工作。然后就不行了,直到我修改保存一个文件,然后重启IDE。一切都被正确命名,它只是正常工作,而不是随机工作。这是 IDE 问题还是项目问题?
我该怎么做才能制作一个在 运行 时可用的 jar 文件?
可能有 3 个问题
- 听起来您需要 IDE 重新编译代码才能在 eclipse 中再次运行?不确定为什么会这样,除非它使用 java 版本 ...
eclipse 通常会根据任何更改重建(例如,您的 space 添加到文件中)
你的eclipseIDE和maven compile使用的是同一个java版本吗?
删除您的 ./target 目录并
将此添加到 Maven pom.xml 以确保您使用的是正确的 java 版本(例如 java 1.8):
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
- 在 jar 构建中包含文件
另外,您的 EmployeeView.fxml 副本应该在 src/main/resources 中,除非您包括:
,否则该文件不会进入您的 jar 文件 <resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
- 该代码在运行时无法在 jar 文件中运行,请参阅以下答案: How to reference a resource file correctly for JAR and Debugging?
并更新您的代码以使用 getREsourceAsStream,例如这是我使用的方法:
public void load(String resource, HashSet<String> hashset) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is_awl = classloader.getResourceAsStream(resource);
BufferedReader reader = new BufferedReader(new InputStreamReader(is_awl));
String line;
try {
while ((line = reader.readLine()) != null) {
hashset.add(line.trim());
}
} catch (IOException e) {
System.err.println("LexicalFeatures can't initialise:"
+ resource + " hashset: "+ e.getMessage());
e.printStackTrace();
}
}
我想出了如何让它发挥作用。真的很烦人但真的很简单。我将在这里写给可能通过搜索引擎找到此页面的任何人。
右键单击项目文件夹 > Maven > 更新项目...
更新所选项目
在本地终端
右击项目文件夹和运行
mvn 安装
并且不要进行 MVN 清理