• Post Reply Bookmark Topic Watch Topic
  • New Topic

jar sub-folder problem  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created a jar file for a small application.
In the jar along with the class files are some data files.
Example: /data/1.csv
When I execute the jar it does not find the file.
If I copy and paste the folder data into the same directory as the jar then it works.
Why is this happening??
Ani idaes?
many thanks
Hugh
 
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "it" does not find the file, what do you mean? Are you using a FileInputStream or FileReader to read the file. If so, you provide a path (or File object) representing the file on the file system, not stuck in some jar file. What you can use is a method of the Class class called getResourceAsStream(). e.g.:
InputStream is = Object.class.getResourceAsStream("/data/1.csv");
Yes, the Object.class part looks weird, but it is necessary. An alternative would be this.getClass(). In this example, it doesn't matter which you use.
 
h slater
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks greg
hugh
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!