• 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

Including / accessing .txt files inside .jar file

 
Sam Smoot
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am creating an application that requires some .txt files to be accessed during processing. Currently, they are in the root of my project directory structure because I can't access them from anywhere else. I know that I should be able to add them to the .jar file and still be able to access them, but I'm not sure which property, build, or resource file I need to modify to make the code in the project actually access the file without it being in the project's root directory.

I am currently using NetBeans 5.0 for this project. Any / all help is greatly appreciated.

Sam
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to read data from a jar full of class files you need to read the data from the classpath as a "resource". It's not really an IDE or tools issue, bu as an example, here's a (slightly simplified) bit of code I use to read the contents of a file on the classpath to a String:



Does that help?
 
Sam Smoot
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess what I'm looking for is what or where I define the resource, either genericaly or through Netbeans, so that as I develop and deploy, everything works the same. I need to make this as much of a single package "tool" as possible.

Thanks.
Sam
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IDE's can make this trickier than it need be.

What I tend to do (I use Eclipse, but I'm guessing netBeans is similar) is:

  • create a directory in the "source" section of my project
  • add the new directory to the IDE classpath
  • place any resource files in it.


  • This enables my code to read the files (using the snippet I posted above) when being unit tested from the IDE.

    Then, for building the deliverable (I use Ant, I don't know what you use), I add a stanza to copy the contents of the resource folder into the compiled classes directory just before building the final jar, war, or whatever.

    Does that help?
     
    Sam Smoot
    Ranch Hand
    Posts: 238
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Took some massaging, but I finally got it to work.. Originally, I was using Buffered Reader / file logic, so that is why it may look a little messy. I'm still cleaning it up, but this helped a lot.




    Oh, and I'm currently using the internal builder in NetBeans to create the .jar file, but I have used ANT as well.

    Thanks again!
    Sam
    [ September 06, 2006: Message edited by: Sam Smoot ]
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic