Search through jar files, find file by name or regular expression

By neokrates, written on July 13, 2010

howto

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 383 views
Rate it
Ad
Poll
  • Which test frameworks do you use?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

Modern projects sometimes have hundreds of .jar files in classpath. But the real quantity of jar packages is thousands, and they are in different versions. So, how about a quick way to find where your particular file is hiding?

Works for

✔ Java

✔ any Linux

1

Script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh
 
DIR=$1
LOOK_FOR=$2
 
for i in `find $DIR -name "*jar"`
do
   jar tvf $i | grep $LOOK_FOR > /dev/null 2>&1;
   if [ $? == 0 ]
   then
     echo "Found \"$LOOK_FOR\" in $i"
     jar tvf $i | grep $LOOK_FOR
   fi
done

 

Takes two parameters, the base path to reduce the search area and what filename you search for:

./search.bash/var/log/TeamCity/ciagent38-3-agent301/maven-repository
ADDITIONAL_SUPP[A-Z]*_en.xml

2

Use cases

Maven

One usecase is maven. Problem with maven is exactly that it stores huge amount of jars.

Use script like

./search.bash /my/path/to/maven-repository 
my_may_be_regular[A-Z]*pattern.class

Global search

It may save your hours of work if you just know where the right .class file. Be warned not to start such search any productive system, it may cause real performance problems…

Missing resources

Java has a great way to load resources. It works like:

mayResource = getClass().getClassLoader().getResource("name")

 
or

mayResource = 
getClass().getClassLoader().getResourceAsStream(resourceName);

 
But what if such code fails with “cannot find the resource ‘you_search_for.properties'” ?
 

👉 The solution is simple:

  • ./search.bash /my/path/to/all/jars/root

you_search_for.properties

  • Dump the classpath, which ClassLoader is using. Something like:
     private void printClasspath() {
 
         System.out.println(" ====== Printing lasspath START =========== ");
         //Get the System Classloader
         ClassLoader sysClassLoader = getClass().getClassLoader();
 
         //Get the URLs
         URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
 
         for(int i=0; i< urls.length; i++)
         {
             System.out.println(urls[i].getFile());
         }
         System.out.println(" ====== Printing lasspath END =========== ");
     }

 

Source

The version here is based on:
http://www.devdaily.com/blog/post/java/how-search-multiple-jar-files-string-pattern

That’s it, have fun!

Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: 0 (from 0 votes)
0 votes 'YES'  0 votes 'NO'


TAGS

SOCIAL
Be Sociable, Share!

INCOMING SEARCH TERMS


Leave a Reply