• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading files from classpath searching with a pattern  RSS feed

 
Walter Angerer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Walter Angerer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Walter Angerer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you're making great progress! Does that give you the list you expect at the end?
 
Walter Angerer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!