• Post Reply Bookmark Topic Watch Topic
  • New Topic

List of property files using classloader  RSS feed

 
Maciej Miklas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a package /config with some property files: *.properties.
I need to find all those files and load them using classloader (getResourceAsStream).
The problem is: how to find my file names?
I am not in jar, and I do not know the system path to create file.
The only thing, that I have is Object....

Thanks,
Maciej
 
Kyle Morgan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File.list()

It's helpful to read the API, you know. You need to override the accept() method in FilenameFilter to only allow .property files.


[ February 19, 2007: Message edited by: Kyle Morgan ]
[ February 19, 2007: Message edited by: Kyle Morgan ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use File.list() if you knew that the property files were ordinary files in a known directory. If you knew they were ordinary files but did not know the directory, maybe you could hack something together by examining the contents of System.getProperty("class.path").

Otherwise, you're a bit stuck. There is no way to ask a generic ClassLoader to list the resources it can see. Presumably, this is because some types of ClassLoader either couldn't do that for technical reasons or don't want to do that for security reasons.

If you know you have a particular kind of ClassLoader, you might find that particular one does support listing. Most don't, though, unless you're writing your own.
 
Maciej Miklas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle Morgan - please read my post - I can not use file, becuase I do not know the path.

I was afraid of this, that I can not do that, but maybie somebady can help me....

Thanks,
Maciej
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Class.getResource() to get a "file:" URL for the jar file, then explicitly open the jar file using the classes in the java.util.jar package. These should let you get a listing of the files you need.

I've never tried this, but you should be able to make it work.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!