• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to read a file packaged in a jar file  RSS feed

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have this embarssing problem, in reading a file which is packaged in a jar file.

I have a project in eclipse, say sample. My project structure is as below



Under this setup, I can simply access the file in Sample.java, as below


and it works like a gem .

Now I exported this project as a runnable jar file and when I looked into the jar file, sample.properties is missing, as expected.

So, in order to have this file available in jar file, I moved sample.properties into src folder. But this time, FileReader fails, as it could not find the file for apparent reasons.

Exception Caused: The property file sample.properties could not be resolv
ed java.io.FileNotFoundException: file:\C:\Projects\Sample.jar!\Sample.properties (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)

I know that the way, I'm accessing the file in FileReader is the problem, but not able to fix it. How can access the file when that is the part of the jar file, to which class belongs. This is apparently the most common way of doing where everything is jar'ed together.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because once it is packaged in a jar it is no longer a file. You can load resources with a class object which will provide the resource as a stream or URL.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!