Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Including / accessing .txt files inside .jar file

 
Sam Smoot
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • 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: 6920
  • Mark post as helpful
  • send pies
  • 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
  • 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: 6920
  • Mark post as helpful
  • send pies
  • 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
    • 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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic