Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How to access a text file inside a JAR

 
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,

I have a text file bundled inside a JAR, how do i access from my code that is also part of the jar?
So for example the package where my code is "com.test.services.LoadRunner.FireCollection" and the text file is also under that package. How do i access it?

-Aditya
 
Marshal
Posts: 79630
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have access to that text file when you are running the unzipped code? How did you gain access? Presumably something like new File("myFile.txt")? Did that path work?

I always thought you are supposed to put files in their own folder and quote the path to that folder when you open the files.
Have you seen the examples in the Java Tutorials? (Try the "creating" page.)
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to address the resource with an URI. The URI for a resource within a jar-file is received using the Class.getResource(String name). (See http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String%29).

If your file is in the package com.test.services.LoadRunner.FireCollection and the file needed is named fire.txt then you get the URI by doing this:



getResource will search from all the classpath starting points. Normally, you put resources like this under a folder of its own. Remember that this is a read only file, but useful when storing templates or default properties.


Edit:
This works, as mentioned below, for resources on the classpath, but not on resources in a Jar. See Ernest addition.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a simple and straightforward answer to this: use the Class.getResource() family of methods. For example, if you have "/data/file.txt" in the jar file, then you can open that file for reading using something like



The nice thing about this is it works perfectly whether the files are in a JAR or not. If they're not, then it works as long as the parent of the data directory is on your classpath.

Edit: Ove's answer won't work if the file is in a JAR, although it can be used to find physical files on the classpath.
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ove Lindström wrote:you get the URI by doing this


Almost right. Resources work with URLs, not URIs.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:Edit: Ove's answer won't work if the file is in a JAR, although it can be used to find physical files on the classpath.



Correct. Thanks for correcting it. Have changed in my post also for the future.


Rob Spoor wrote:
Almost right. Resources work with URLs, not URIs.



Note to self: Read the Javadoc in a correct way. ;)
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys,

FileReader paramReader = new FileReader("com/test/services/LoadRunner/FireCollection/fire.txt"); and will this file be read if its inside com.test.services.LoadRunner.FireCollection package?

-Aditya
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aditya Sirohi wrote:
FileReader paramReader = new FileReader("com/test/services/LoadRunner/FireCollection/fire.txt"); and will this file be read if its inside com.test.services.LoadRunner.FireCollection package?



Not if the files are in a JAR, and only if the directory "com" is a subdirectory of the current working directory.

Use this instead: it will find a file anywhere on the classpath, even inside a JAR:



 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic