| Author |
Including / accessing .txt files inside .jar file
|
Sam Smoot
Ranch Hand
Joined: Apr 18, 2002
Posts: 238
|
|
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
|
CNSS/NSA Infosec Professional,<br />Software Engineer
|
 |
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
|
|
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?
|
A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
|
 |
Sam Smoot
Ranch Hand
Joined: Apr 18, 2002
Posts: 238
|
|
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
Joined: Jan 07, 1999
Posts: 6913
|
|
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 projectadd the new directory to the IDE classpathplace 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
Joined: Apr 18, 2002
Posts: 238
|
|
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 ]
|
 |
 |
|
|
subject: Including / accessing .txt files inside .jar file
|
|
|