当我运行我的 jar 文件时,它不起作用,因为它找不到 books.csv,但它存在于指定的源中

问题描述 投票:0回答:0

我的 jar 文件不会运行。它在引用主包中的文件时出错。

java-jar IA.jar

file not found
java.io.FileNotFoundException: src/main/books.csv (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:158)
    at java.base/java.util.Scanner.<init>(Scanner.java:641)
    at main.Library.loadScanner(Library.java:98)
    at main.Library.loadBooks(Library.java:119)
    at main.Library.<init>(Library.java:25)
    at main.Main.main(Main.java:17)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Scanner.hasNext()" because "scanner" is null
    at main.Library.loadScanner(Library.java:107)
    at main.Library.loadBooks(Library.java:119)
    at main.Library.<init>(Library.java:25)
    at main.Main.main(Main.java:17)

主类看起来像这样:

public class Main {
    private final static String root = "src/main/";
    public static void main(String[] args) throws IOException {
        String[] dirs = {root + "books.csv", root + "teacher.csv", root + "users.csv"};
        Library library = new Library(dirs); // libary instance

        new HomePage(library); // open GUI
    }
}

库方法 loadScanner() 用于打开充当一种虚拟数据库的 .csv 文件。

private ArrayList<String[]> loadScanner(String dir) {
        File myFile1 = new File(dir);
        Scanner scanner = null;
        try {
            scanner = new Scanner(myFile1);
            scanner.useDelimiter(",");
        } catch (FileNotFoundException e) {
            System.out.println("file not found");
            e.printStackTrace();
        }

        ArrayList<String[]> data = new ArrayList<>();

        while (scanner.hasNext()) {

            String line = scanner.next();

            String[] r = line.trim().split(",");
            data.add(r);
        }
        return data;
    }
java csv file jar java.util.scanner
© www.soinside.com 2019 - 2024. All rights reserved.