我有一些文本文件和几张我在代码中加载的图片。 当我在NetBeans中运行程序时它们工作正常,但是当我清理并构建项目时,生成的jar文件不会运行,并返回一个异常,表明文件没有被读取。
这是我加载文件的方式:
public static ArrayList<ArrayList<String>> readTextFile(String filePath){
ArrayList<ArrayList<String>> data = new ArrayList();
String line;
BufferedReader br = null;
try{
//br = new BufferedReader(new FileReader(filePath));
br = new BufferedReader(new InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream(filePath)));
while((line = br.readLine()) != null){
ArrayList<String> temp = new ArrayList<>();
Scanner scanner = new Scanner(line);
scanner.useDelimiter(Pattern.compile(";"));
while(scanner.hasNext()){
temp.add(scanner.next());
}
data.add(temp);
}
}catch (FileNotFoundException e){
}catch (IOException e){
}finally {
try {
if (br != null) {
br.close();
}
}catch (IOException e) {
}
}
return data;
}
这就是我调用方法的方式:
ArrayList<ArrayList<String>> loadedData = readTextFile("WeaponVendor.txt");
顺便说一句,我将所需文件添加到Project-&gt; Properties-&gt; Libraries中。
如果它有用,这是我在运行jar文件时得到的异常:
Exception in Application constructor
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: Unable to construct Application instance: class trabalhoPoo.Game
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at trabalhoPoo.util.fileManager.readTextFile(fileManager.java:35)
at trabalhoPoo.GameData.loadWeaponVendor(GameData.java:126)
at trabalhoPoo.GameData.<init>(GameData.java:42)
at trabalhoPoo.Game.<init>(Game.java:47)
... 13 more
Exception running application trabalhoPoo.Game