IntelliJ Java IDE
The moose likes I/O and Streams and the fly likes Reading files from classpath searching with a pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Reading files from classpath searching with a pattern" Watch "Reading files from classpath searching with a pattern" New topic
Author

Reading files from classpath searching with a pattern

Walter Angerer
Greenhorn

Joined: May 09, 2007
Posts: 5
Hello,

I want to read a file zipped in a jar and loaded through the jar into the classpath.
The problem is that I want to find several files loaded in the classpath giving a pattern for example *text.xml. This pattern should lead to a result like follows:

com.company.test1.test.xml
com.company.test2.test.xml
and so on...

Is there a possibility to implement such a resource search in the classpath? And if it is so how could I implement it?

Would be nice to get an answer.

Kind Regards,
Walter
PS: I am not a native speaker and therefore I apologize the bad english.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch! I'm in awe of anyone who manages English as a 2nd (or 5th or whatever) language, so you're doing great as far as I'm concerned.

See if this is right: You have a jar that's on the classpath. In the jar is a zip file. In the zip file there are a bunch of files you wan to read?

Seems like we have to get the classpath, find the jar we care about, open the jar and get the zip we're interested in, open the zip and iterate through the list of files. Does that seem right? Where are you stuck?

Now, if I'm way off base, showing us a little bit of code that almost works is a cool way clear up the question.
[ May 14, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Walter Angerer
Greenhorn

Joined: May 09, 2007
Posts: 5
Hi,

thanks for the response.

First of all there is no zip file in the jar. If I say zipped in a jar I mean that the files resides in the jar directly. As far as I know jar files are more or less zip files therefore I said zipped :-)

The task is as you said to find the jar file and locate in this jar files the files residing in it.
The ClassLoader.getResource() method doesn't help me much cause I have to know the package name and resource name exactly and that's my problem.
In the best case I know the jar file where the file are in. In the worst case I don't know nothing else than the file name or a piece of the filename.
Therefore I try to find a solution to go through the classpath resources (maybe iterate through it) and try to find the files. Performance isn't relevant cause it is needed to find files containing test cases meaning this piece of code would never go into production.

I have the problem that I don't find the correct entry point for that meaning how can I get a list of resources in the classpath?

Maybe somebody knows a solution or the correct call of the java "buildin" classes?

Kind Regards,
Walter
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok!
i do not have a deep knowledge about jar files but still i can help you to some extent.You refer to the below shown link.In this article you read the
topic
1)Retrieving resources from JAR files.
the code is given there,may be it proves helpful to you.
http://java.sun.com/j2se/1.4.2/docs/guide/jws/developersguide/development.html
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Hi Walter. Here's a quick trick to get the classpath and split it into parts:

You can then loop through this array to examine each element to determine if it's a jar file. When you find a jar file, you can open it using a new JarFile, then use entries() to get an Enumeration(ugh) of the jar entries. For each entry, you can get the name and see if it ends in ".xml" or whatever. If it does, get the InputStream associated with the entry, read it, and do whatever you need to do with the data from the file. Does that help?


"I'm not back." - Bill Harding, Twister
Walter Angerer
Greenhorn

Joined: May 09, 2007
Posts: 5
@dhwani: that's the right direction. I have something similar but I need the last kick to find according a pattern.

@Jim: ok, that could also be a possible solution and means more coding work cause I have to open each jar file and must have a look into it. It's a makeable solution but I thought I get more support through the java mechanisms itself.

At the moment I am experimenting with following method:



The problem is that the filename pattern doesn't work cause the class loader needs the exact name of the resource.

Hmm, I will experiment again. Maybe I will find a solution. If I had found one I will post it here.

Thanks for the replies.

Kind Regards,
Walter
Walter Angerer
Greenhorn

Joined: May 09, 2007
Posts: 5
Hi folks,

I have implemented a first draft:

the classpath searcher class:


a simple unit test class using the class path searcher:



Maybe there are some suggestions to improve it

Kind Regards,
Walter
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Looks like you're making great progress! Does that give you the list you expect at the end?
Walter Angerer
Greenhorn

Joined: May 09, 2007
Posts: 5
Yes I get the list that I expect. I have now only the small problem that the maven surefire plugin doesn't append the "target/test-classes" and "target/classes" to the system property "java.class.path". Therefore executing my unit test under maven fails.

I am searching now for a solution that the surefire plugin appends also the "target/classes" and "target/test-classes" to the "java.class.path" system property.
Sean Foreman
Greenhorn

Joined: Jan 13, 2011
Posts: 1
This worked great within eclipse. Not so much in resin. I had to change it to interrogate the classloader. It appears the classloader had everything from the classpath but I kept the classpath code just in case.

 
 
subject: Reading files from classpath searching with a pattern
 
Threads others viewed
How to set classpath in Windows 98
add jar files in a directory to classpath
build problems and relative paths
MDB, Spring, deployment related issue
Stored java procedure error on oracle client
developer file tools