Home > Mobile >  Java code not reading CSVs after being compiled
Java code not reading CSVs after being compiled

Time:12-21

So I have a finished java project. It can read CSVs when I press run class on VSCODE. The App class is the main class. However when I compile the project and type "Java App" on the terminal it can't find the location of the CSV. I'm not sure why. This is the error I get

Exception in thread "main" java.io.FileNotFoundException: AIR-QUALITY-PROJECT/lib/1year.csv (No such file or directory)
        at java.base/java.io.FileInputStream.open0(Native Method)
        at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
        at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
        at DataHandler.readDataFromCSV(DataHandler.java:18)
        at App.main(App.java:14)

The CSV is in lib and I'm running App from the src. Again this CSV runs fine when I run the class on VSCODE

I tried putting the 1year.csv on the root, in the src, everything!!! it doesn't work when I compile it

This how I am calling the path

private static final String PATH = "AIR-QUALITY-PROJECT/lib/1year.csv";

Again runs perfectly when I press run class on VSCODE

App.java is in SRC and 1year.csv is in the lib folder

CodePudding user response:

You are using a relative path. That means relative to where the application runs.

If you want to have a reliable application you should use an absolut path, and the best would probably be if you configure or pass the path when running the application.

CodePudding user response:

In such cases it is useful to log what the currentPath is while the program is running. You could do this e.g. like this

System.out.println("Current path is: "   new File("").getAbsolutePath());

All your relative paths must be relative to this path.


P.s. There are various methods to get the currentPath in java. This is just one of them.

  • Related