Java 代码工作正常,然后在maven安装之后,jar和IDE中都会出现错误
应用程序启动方法中出现异常:Java 代码工作正常,然后在maven安装之后,jar和IDE中都会出现错误,java,eclipse,maven,fxml,Java,Eclipse,Maven,Fxml,应用程序启动方法中出现异常: 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.
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$launchApplication$154(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$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Exception running application edu.citytech.view.EmployeeMain
下面是使用主方法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);
}
}
解决这个问题的方法是向任何文件添加一个空间并保存它,然后重新启动EclipseIDE,并从IDE再次运行它。当我运行mvn clean和mvn install,然后运行它创建的jar文件时,会破坏它。我们班上每个人都有这个问题,老师不知道如何解决。它只对某些人有效,对其他人无效。然而这一次,无论我怎么尝试,这都不起作用。在我使用maven创建jar之前,它只能在IDE中工作。直到我修改并保存一个文件,然后重新启动IDE,它才会工作。所有东西都正确命名,它只是工作,而不是随机工作。这是IDE问题还是项目问题
我该怎么做才能使jar文件在运行时工作?3问题
1.8
1.8
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
src/main/resources
**/*
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();
}
}
公共无效加载(字符串资源,哈希集){
ClassLoader ClassLoader=Thread.currentThread().getContextClassLoader();
InputStream是_awl=classloader.getResourceAsStream(资源);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is_awl));
弦线;
试一试{
而((line=reader.readLine())!=null){
add(line.trim());
}
}捕获(IOE异常){
System.err.println(“词典功能无法初始化:”
+resource+“hashset:+e.getMessage());
e、 printStackTrace();
}
}
我找到了让它工作的方法。很烦人,但很简单。我将在这里为任何可能通过搜索引擎找到此页面的人编写
并且不要进行MVN清理检查
EmployeeView.fxml
是否包含在点击非常接近的jar
中。它实际上不在罐子里。除非我将其复制到src/main/resources。但是它仍然不起作用,我认为它必须与主方法所在的包在同一个包中。
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();
}
}