1
votes

I am getting these file not found exceptions when running the .jar, but not when running my application in netbeans. Here are the error messages, with a current directory print at the top.

adam@Adam ~/Dropbox/linuxWorkspace/Netbeans/EveMarketCalculater/dist $ java -jar "EveMarketCalculater.jar"

Current sys dir: /home/adam/Dropbox/linuxWorkspace/Netbeans/EveMarketCalculater/dist

java.io.FileNotFoundException: file:/home/adam/Dropbox/linuxWorkspace/Netbeans/EveMarketCalculater/dist/EveMarketCalculater.jar!/inputOutput/TypeID.csv (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:93) at java.io.FileReader.(FileReader.java:58) at inputOutput.csvParser.readFile(csvParser.java:54) at inputOutput.csvParser.(csvParser.java:24) at evemarketcalculater.EveMarketCalculater.main(EveMarketCalculater.java:39)

java.io.FileNotFoundException: file:/home/adam/Dropbox/linuxWorkspace/Netbeans/EveMarketCalculater/dist/EveMarketCalculater.jar!/inputOutput/Regions.csv (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:93) at java.io.FileReader.(FileReader.java:58) at inputOutput.csvParser.readFile(csvParser.java:54) at inputOutput.csvParser.(csvParser.java:24) at evemarketcalculater.EveMarketCalculater.main(EveMarketCalculater.java:42)

java.io.FileNotFoundException: file:/home/adam/Dropbox/linuxWorkspace/Netbeans/EveMarketCalculater/dist/EveMarketCalculater.jar!/inputOutput/SolarSystems.csv (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:93) at java.io.FileReader.(FileReader.java:58) at inputOutput.csvParser.readFile(csvParser.java:54) at inputOutput.csvParser.(csvParser.java:24) at evemarketcalculater.EveMarketCalculater.main(EveMarketCalculater.java:45)

java.io.FileNotFoundException: file:/home/adam/Dropbox/linuxWorkspace/Netbeans/EveMarketCalculater/dist/EveMarketCalculater.jar!/inputOutput/MyOrders.csv (No such file or directory) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.(FileInputStream.java:138) at java.io.FileInputStream.(FileInputStream.java:93) at java.io.FileReader.(FileReader.java:58) at inputOutput.csvParser.readFile(csvParser.java:96) at inputOutput.csvParser.(csvParser.java:29) at evemarketcalculater.EveMarketCalculater.main(EveMarketCalculater.java:48)

Here is some source code of how I am accessing these files:

    public final void readFile(String file, int index1, int index2)
{
    BufferedReader br = null;
    String line = "";
    String cvsSplit = ",";
    idList = new ArrayList();

    try
    {
       URL url = csvParser.class.getResource(file);

       br = new BufferedReader(new FileReader(url.getPath()));

       while((line = br.readLine()) != null)
       {
           String[] item = line.split(cvsSplit);

           idList.add(item[index1].replace("\"",""));
           idList.add(item[index2].replace("\"",""));
       }
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

    if(br != null)
    {
        try
        {
            br.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

}


    public static void writeMyOrders()
{
    URL url = csvParser.class.getResource("MyOrders.csv");

    FileWriter w = null;
    csvWriter c = null;

    try
    {
        // Empty file cotents
        PrintWriter writer = new PrintWriter(url.getPath());
        writer.close();

        w = new FileWriter(url.getPath());

        c = new csvWriter();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }

    for(int i = 0; i < myOrders.size(); i++)
    {
        try
        {
            c.writeLine(w, myOrders.get(i));
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    if(w != null)
        try
        {
            w.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

}

Any clue why this isn't working? It's obviously tracing the right file path, and the files exist inside of the .jar file at these paths. I'm running in a Linux command line if that matters.

Here is proof that the files exist.

picture of .jar directory

2

2 Answers

0
votes

Try this

 br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(file)));
0
votes

Try using the following statement while creating BufferedReader:

br = new BufferedReader(new InputStreamReader(csvParser.class.getResourceAsStream(file)));

Class.getResource() returns you the URL while Class.getResourceAsStream() gives you InputStream from where you can read the file contents directly.

Explanation From the error message, it is visible that your application is unable to find the file file:/home/adam/Dropbox/linuxWorkspace/Netbeans/EveMarketCalculater/dist/EveMarketCalculater.jar!/inputOutput/TypeID.csv. To verify that, you can try opening the mentioned file with any other application like gedit or chrome or vi. If you can open it, your application can also do that.

You got such a path from the URL returned from getResource(). When it is getResourceAsStream(), you are provided with an InputStream with the resource you are looking for.

You have the option to update the file in a jar file. Haven't tried by myself, but you may refer to Can a Jar File be updated programmatically without rewriting the whole file?