Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

not finding file from jar file

 
Kyle Buttress
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application that works correctly when used in this form java myApp. it is able to real the files listed in a sub directory with this structure

myApp.class
filedir/filetoread.txt

however when I jar these with the following

jar -cf myApp.jar *
jar umf ../manifest.txt myApp.jar

maifest.txt has the following information

Class-Path: myApp.jar filedir/
Main-Class: myApp

when I run the app with

java -jar myApp

the applicationn runs but cannot find the filetoread.txt file.

----> here is my read file method
private String readFile(String filename) {

String nextLine = "";
StringBuffer sb = new StringBuffer();

java.io.File f = new java.io.File(filename);
try {
String lineSep = System.getProperty("line.separator");
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.FileReader(f));

while ((nextLine = br.readLine()) != null) {
sb.append(nextLine);
//
// note:
// BufferedReader strips the EOL character.
//
sb.append(lineSep);
}
}
catch(Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}

any assistance would be fantastic

regards

Kyle
 
Kyle Buttress
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found my own fix for the problem. It has to do with the way the file is read from the jar file.

here is my readfile method for reference

it reads the file and returns the contents as a string so I can display on a jTextArea


private String readFile(String filename) {

String thisLine;
StringBuffer sb = new StringBuffer();
String lineSep = System.getProperty("line.separator");
try {
java.io.InputStream is = getClass().getResourceAsStream(filename);
java.io.BufferedReader br = new java.io.BufferedReader
(new java.io.InputStreamReader(is));
while ((thisLine = br.readLine()) != null) {
sb.append(thisLine);

sb.append(lineSep);
}
}
catch(Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!