user5555231
user5555231

Reputation: 77

Java Filenotfound exception in jar file

i wrote a program, but when my friends try to execute it it throw filenotfound exception, but the file is exist, here is my code, and in the folder have lib folder, the jar file and the "csv fajlok" and in the csv fajlok folder there is the 2 csv file

String csvFile = "csv fajlok\\pontcsoport.csv";
    BufferedReader br = null;
    String line = "";

    try {
    br = new BufferedReader(new FileReader(csvFile));

        while ((line = br.readLine()) != null) {

        String[] pontGroupLine = line.split(";");
        String[] price_split = pontGroupLine[1].split(" ");

            try{
               doubleDTList.add(Double.parseDouble(price_split[0]));
            }catch(NumberFormatException e){
             }                   
        }
    }catch (FileNotFoundException e) {
        JOptionPane.showMessageDialog(null, "Nem található a pontcsoport fájl (/csv fajlok)");
    }catch (IOException e) {
    }finally {
        if (br != null) {
        try {
            br.close();
        }catch (IOException e) {
         }
        }
    }

Upvotes: 0

Views: 205

Answers (2)

eg04lt3r
eg04lt3r

Reputation: 2610

Better to use Java NIO2 to read the file content:

List<String> lines = Files.readAllLines(Paths.get("path-to-file"), charset);

It's allow to avoid using while loop with redundant readers.

What is the OS installed on failed notebooks?

Also try to change folder name using '_' instead of space. I think it's the main reason of the issue.

Upvotes: 1

Mario
Mario

Reputation: 1781

If the file is outside the jar file, you should put an absolute path, or the "csv fajlok" folder should be in the same folder where you execute the jar file.

If the file is inside the jar file, you cannot access to it as a file but as a Stream, with the method Class.getResourceAsStream(String path).

Upvotes: 2

Related Questions