0

I am writing a Java program using NetBeans that times the process of developing black and white film. One of the options allows you to set the times manually and the other allows you to select the chosen film and developer to calculate the required times from a CSV file that is loaded into an array.

There are 3 files that are loaded, filmdb.csv, masterdb.csv and usersettings.csv. The first two are loaded purely for reading and the third file is loaded and can be written too to save the users default settings.

All 3 files are stored in the project directory and loaded in a similar way to the following code and called from main:

static String[] filmArray;
static int filmRows = 125;
int selectedDevTime;
int tempDevTime;
int minDevTime;
int secDevTime;
public int count = -1;

static void createFilmArray() {
    filmArray = new String[filmRows];

    Scanner scanLn = null;
    int Rowc = 0;
    int Row = 0;
    String InputLine = "";
    String filmFileName;

    filmFileName = "filmdb.csv";

    System.out.println("\n****** Setup Film Array ******");

    try {
        scanLn = new Scanner(new BufferedReader(new FileReader(filmFileName)));

        while (scanLn.hasNextLine()) {
            InputLine = scanLn.nextLine();
            filmArray [Rowc] = InputLine;
            Rowc++;
        }
    } catch (Exception e) {
        System.out.println(e);
    }        
}

When I press the run button in NetBeans all works well, the files are all loaded and stored to the appropriate array but when I build the file as a .jar the files are not loaded, I have tried copying the 3 files to the same directory as the .jar as well as importing the 3 files into the .jar archive but to no joy.

Is there a specific location where the files should be placed, or should they be imported in a different way?

I would rather not load them from an absolute directory as for example a Windows user may have the files stored in C:\users\somebody\devtimer\file.csv and a Linux user may have the files stored in /home/somebody/devtimer/file.csv.

Cheers Jim

2 Answers 2

1

your files got packaged into the *.jar once a file is in the jar you cannot accessit as a file using a path (since, if you think about it, its no longer a file. its inside another file, the jar file).

you need to do something like this:

InsputStream csvStream = this.getClass().getResourceAsStream("filmdb.csv");

then you can pass this input stream to your csv parser. you will not be able to make any modifications to the fhile though. any file you want to change you will need to store outside of your jar.

as for file paths, things like new File("somename") are resolved relative to the current working directory. if you want to know where your root is try something like:

System.err.println(new File(".").getCanonicalPath());

this will be very sensitive to what the working directory was when your application was executed, which in turn depends on how exactly it was executed etc.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help, being able to modify one of the files is required. How would I link to a file outside of the .jar?
@TheVDM - access to files outside the jar (which are real files, on the filesystem) is the same as your code. i suspect the reason you stop finding your files is because the current working directory is different when your jar is executed. try printing out the CWD and see
Use the resource that is intended for writing as read-only template. Copy it to a File under the user directory and use that. System.getProperty("user.home") is the home directory.
0

If you are running the jar from the same directory the csv files are in then it should be loading. There might be some issue though if you just double click on the jar. You could create a new File represented by the filename and check the absolute path to find out where the jar is running from.

If you want to include the csv inside the jar (cleaner for distribution), you need to load the files in a different way. See this question for a nice example.

1 Comment

Thank you, I have tried running the .jar file from the terminal using 'java -jar devtimer.jar' which is now loading the external CSV files, so my best bet would be to create a bash script to run the jar file from it's current directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.