user3471443 user3471443 - 3 months ago 17
Linux Question

Java .jar file not finding system file when it is there and following the correct path

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

Answer

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.

Comments