• 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

How do I refer to a text data file so a .jar can find it?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to ask such a basic question, but I have a simple app. reading data from a text file I have put in the src directory resources section as detailed below.



This works fine when running the project from within my IDE. However, when I build the project and try and run it from the .jar the program fails to find the file (I can see the error when I run it from the command line, because I've put the code line I detailed above within an appropriate try - catch block and it throws a file not found error). I've tried editing the Path but nothing seems to work, even though I can see the file within the .jar using winzip. There are also two other (empty) resources folders within the .jar, one at the top level and one below the level I've stored the file at (i.e. within resources!), however I chose the mid level one because all the other resources like .jpg images are there and the program seems to find them without trouble. I thought, just removing the src\\ ought to do the trick because the path within the .jar is solowarforedadhopponent\resources\MasteryCardData.esd, but this stops the program functioning both within the IDE and as a .jar.

So, my question is this: Where should this data file actually go and how can I refer to the file so that the program runs correctly both inside the IDE and as a .jar?

References to other threads, articles and tutorials all welcome.

Many Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Hunt wrote:
I've tried editing the Path but nothing seems to work, even though I can see the file within the .jar using winzip.



The File class is used to access files in the filesystem. It is not used to get resource files from within a jar file, in the class path. To do that take a look at the getResourceXXX() methods from the Class class (google java resource files).

Henry
 
Simon Hunt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry

Many thanks for the initial clues, but I'm still struggling.


I'm not sure what you mean by the getResourceXXX() methods. When I look at the Class class in the API it only lists two similar methods:
1. getResource(String name) which returns a URL object.
To test this I put a "test print" line into my code
which gives something that looks correct but doesn't work - even if I make the string "resources\\MasteryCardData.esd". (Not sure about \\ and / either - using \\ inserts %5c into the string in this case)

2. getResourceAsStream(String name) returns an InputStream object which doesn't have a readLine() method which I need to parse my csv file.

My problem seems to be that ultimately I need a File object to pass to my FileReader to pass to my BufferedReader for that.



Just in case it's relevant, here's the code that follows the file open:


Many Thanks for any additional clues.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try "/solowarforedadhopponent/resources/MasteryCardData.esd"
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Hunt wrote:
2. getResourceAsStream(String name) returns an InputStream object which doesn't have a readLine() method which I need to parse my csv file.

My problem seems to be that ultimately I need a File object to pass to my FileReader to pass to my BufferedReader for that.



First of all, why do you absolutely need the readLine() method? Are you saying that you can't parse it, if the BufferedReader class isn't available? Second, why do you need a FileReader ? Are you saying that you can't get a BufferedReader without a FileReader ?

Henry
 
Simon Hunt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gentlemen

Ulf - many thanks for the suggestion, but I'm afraid it causes the program to fail in both the IDE (Netbeans) and in the .jar. Curiously enough "src/solowarforedadhopponent/resources/MasteryCardData.esd" works just as well in the IDE as "src\\solowarforedadhopponent\\resources\\MasteryCardData.esd". As I said in my last post, I really don't understand the \\ vs / thing, but I hope it's not key.

Henry - it seems from your tone that you're finding this exchange irritating, for which I apologise, but the reason I'm posting to this forum is that I'm very much a Java beginner despite having joined this forum some years ago. I'm a hobbyist programmer, and I only usually get to programming about once a year, and it usually involves me largely re-learning Java from scratch (I use "Head First Java" mainly because it's fun).

Henry Wong wrote: First of all, why do you absolutely need the readLine() method? Are you saying that you can't parse it, if the BufferedReader class isn't available?

I'm saying I don't know how to parse it if the BufferedReader class isn't available. I'm sure that there are plenty of other ways of parsing a file I don't know about. During my scouring of the API I found a bunch of bit-wise methods in the getResourceAsStream section, so I know that's possible to do it bit-wise, but that seems a crazy way to tackle a .csv file; the readLine() method gives me a line at a time as a string which I know how to manipulate satisfactorily.

Essentially I've written some code to parse the .csv file which works fine, and re-writing it to solve what I perceive (albeit probably wrongly) as a path issue looks like tail wagging dog. That said, there are probably more elegant ways of doing this I don't know about as well, however I'm relying on a code segment from the "Head First Java" book, which only mentions this way of handling text files, so it's the only method I know.

Here's the full code section (I was avoiding posting this because it's long but I think the time has come) so you can see what I'm up to:
I've left in all the ideas I've tried and failed with, as comments, to avoid re-treading the same ground; happily it also allows you to see what I've tried too.

Henry Wong wrote:Second, why do you need a FileReader ? Are you saying that you can't get a BufferedReader without a FileReader ?

I'm really not asserting anything. It just appears from the section of the book and the code examples that a BufferedReader takes a FileReader as a constructor argument.
*looks at API*
The API suggests I simply need a Reader of which BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader are all sub-classes. It's interesting that FileReader is not on that list.

However, InputStreamReader looks promising...

I'll try this:
This works inside the IDE fine... but not in the .jar

Okay Simon, don't panic yet. Try "resources/MasteryCardData.esd"
This works inside the IDE fine... AND AS A .jar

Yes! He can be taught!

Many Thanks Henry
p.s. I still don't know why the IDE takes \\ but the jar only likes /?


 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simon Hunt wrote:
Henry - it seems from your tone that you're finding this exchange irritating, for which I apologise, but the reason I'm posting to this forum is that I'm very much a Java beginner despite having joined this forum some years ago. I'm a hobbyist programmer, and I only usually get to programming about once a year, and it usually involves me largely re-learning Java from scratch (I use "Head First Java" mainly because it's fun).



Nope. No irritation whatsoever. It is just a bunch of hints phrased in a form of a question. Pretty common technique for me.... And trust me, it takes a *LOT* of effort to get me irritated....

Henry
 
Simon Hunt
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Nope. No irritation whatsoever. It is just a bunch of hints phrased in a form of a question. Pretty common technique for me.... And trust me, it takes a *LOT* of effort to get me irritated....



All's good then. It just goes to show how tricky conversations are without voice tone and body-language cues.

Many thanks for leading me to the answers. I admit freely to thinking "Just give me the damn fish!", but the question phrased hints really did work and it really is the best way.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic