• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

not finding file from jar file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic