Java Netbeans未在内置jar文件中包含资源

Java Netbeans未在内置jar文件中包含资源,java,Java,我在代码中加载了一些文本文件和一些图像。 当我在NetBeans中运行程序时,它们工作得很好,但是当我清理并构建项目时,生成的jar文件不会运行,并返回一个异常,显示文件没有被读取 这是我加载文件的方式: public static ArrayList<ArrayList<String>> readTextFile(String filePath){ ArrayList<ArrayList<String>> data = new A

我在代码中加载了一些文本文件和一些图像。 当我在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;
    }
publicstaticarraylistreadtextfile(字符串文件路径){
ArrayList数据=新的ArrayList();
弦线;
BufferedReader br=null;
试一试{
//br=新的BufferedReader(新的文件读取器(文件路径));
br=新的BufferedReader(新的InputStreamReader(ClassLoader.getSystemClassLoader().getResourceAsStream(filePath));
而((line=br.readLine())!=null){
ArrayList temp=新的ArrayList();
扫描仪=新扫描仪(行);
scanner.useDelimiter(Pattern.compile(“;”);
while(scanner.hasNext()){
临时添加(scanner.next());
}
添加数据(临时);
}
}catch(filenotfounde异常){
}捕获(IOE异常){
}最后{
试一试{
如果(br!=null){
br.close();
}
}捕获(IOE异常){
}
}
返回数据;
}
这就是我调用方法的方式:

ArrayList<ArrayList<String>> loadedData = readTextFile("WeaponVendor.txt");
ArrayList loadedData=readTextFile(“WeaponVendor.txt”);
顺便说一下,我通过将所需文件添加到Project->Properties->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
应用程序构造函数中的异常 java.lang.reflect.InvocationTargetException 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源) 位于java.lang.reflect.Method.invoke(未知源) 位于com.sun.javafx.application.LaunchImpl.launchApplicationWithArgs(LaunchImpl.java:389) 位于com.sun.javafx.application.LaunchImpl.launchApplication(LaunchImpl.java:328) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(未知源) 在sun.reflect.DelegatingMethodAccessorImpl.invoke处(未知源) 位于java.lang.reflect.Method.invoke(未知源) 位于sun.launcher.launchelper$FXHelper.main(未知源) 原因:java.lang.RuntimeException:无法构造应用程序实例:类trabalhoPoo.Game 位于com.sun.javafx.application.LaunchImpl.launchApplication1(LaunchImpl.java:907) 在com.sun.javafx.application.launchempl.lambda$launchApplication$155(launchempl.java:182) 位于java.lang.Thread.run(未知源) 原因:java.lang.reflect.InvocationTargetException 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源) 位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源) 位于java.lang.reflect.Constructor.newInstance(未知源) 在com.sun.javafx.application.launchempl.lambda$launchApplication1$161(launchempl.java:819) 位于com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 位于com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 位于java.security.AccessController.doPrivileged(本机方法) 位于com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95) 在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) 位于com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) ... 还有一个 原因:java.lang.NullPointerException 位于java.io.Reader。(未知源) 位于java.io.InputStreamReader。(未知源) 位于trabalhoPoo.util.fileManager.readTextFile(fileManager.java:35) 在trabalhoPoo.GameData.loadWeaponVendor(GameData.java:126) 在trabalhoPoo.GameData.(GameData.java:42) 在trabalhoPoo.Game.(Game.java:47) ... 还有13个 运行应用程序trabalhoPoo.Game时出现异常
在NetBeans中运行程序时,它将为您处理类路径。当从命令行运行它时,您需要设置类路径,以便Java知道您需要的任何其他JAR文件的位置。你可能还想考虑把你的项目切换到Maven,这将(可以说)使所有这一切变得简单一点。但是我只是意识到了这个路径问题,没有时间改变这个项目。