0

My jar file won't run. It gives an error in reference to a file that is in the main package.

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)

The main class looks like this:

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
    }
}

The library method loadScanner() is used to open the .csv files which act as a sort of dummy database.

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;
    }
  • What's the code? – Erwin Apr 04 '23 at 04:21
  • @Erwin ```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 } }``` – Edwin Ryerson Apr 04 '23 at 04:22
  • put it in the question, I can't see it like this – Erwin Apr 04 '23 at 04:24
  • what is the line that caused the error? `at main.Library.loadScanner(Library.java:98)` – Erwin Apr 04 '23 at 04:27
  • At runtime you do not have a src/main folder. Use Class.getRessource() to load the file, if it is in the classpath – Jens Apr 04 '23 at 04:30
  • @Jens How do I fix that? – Edwin Ryerson Apr 04 '23 at 04:31
  • @Jens I am unfamiliar with Class.getResource(). How would I use it in my main class? – Edwin Ryerson Apr 04 '23 at 04:39
  • Does this answer your question? [Reading a resource file from within jar](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar) – experiment unit 1998X Apr 04 '23 at 05:14
  • 1
    @Edwin Ryerson, First decide what you want to achieve? (a) Jar reads a book.csv file placed in a certain directory (b) Jar reads a book.csv file placed in the same directory as Jar (c ) Jar reads a book.csv file inside the Jar. – life888888 Apr 04 '23 at 06:36

0 Answers0