Search through jar files, find file by name or regular expressionBy neokrates, written on July 13, 2010 |
howto |
- neokrates
- Email: uwarov@yahoo.com
- Website: http://www.thinkplexx.com
- Join date: 05-31-09
- Posts: 20
What would describe state of logging infrastructure of your enterprise best?
- We have no logging infrastructure (100%, 4 Votes)
- Tons of logs and no good way to handle them (0%, 0 Votes)
- Tons of logs and finding the problem might take days (0%, 0 Votes)
- No log system setup, we loose important data (0%, 0 Votes)
- We can extract the info from log files in acceptable time (0%, 0 Votes)
- Problems can be found quickly thanks to our tuned logging and analyzing system (0%, 0 Votes)
- System reacts to the problems by sending messages to the teams most qualified (0%, 0 Votes)
Total Voters: 4
Loading ...
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!
|
LEARN MORE (amazon bookstore)
|
|
TAGS
|
|
SOCIAL
|


















