Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux cmd to search for a class file among jars irrespective of jar path

Tags:

linux

command

I want to search for a particular class file among many jar files without giving the location of each jar file.

Is this possible with a simple command?

I tried this command:

grep Hello.class *.jar 

Which did not return a list of jars containing the class. Then I ran the command:

grep Hello.class /full/path/of/jar/file/*.jar 

Which did return the relevant jar file. Is there a better way?

like image 453
AlwaysALearner Avatar asked Jan 17 '13 07:01

AlwaysALearner


People also ask

How do I find a class file in a bunch of jars?

this is a very simple and useful tool for windows. A simple exe file you click on, give it a directory to search in, a class name and it will find the jar file that contains that class.

How do I search for a class file in Linux?

To find the . jar files that contain a class, you can use the FindClass.sh script. First go to a UNIX installation of Sterling Platform/MCF. If the FindClass.sh script already exists it should be in your $YFS_HOME directory or your $YFS_HOME/lib directory.

How do I search for a specific jar file in Linux?

find ./ -name "filename" or you can do something like find ./ -name "*. jar" to find all the files with the . jar extension. You can also do find ./ -name "*.


2 Answers

Most of the solutions are directly using grep command to find the class. However, it would not give you the package name of the class. Also if the jar is compressed, grep will not work.

This solution is using jar command to list the contents of the file and grep the class you are looking for.

It will print out the class with package name and also the jar file name.

find . -type f -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep Hello.class &&  echo {}' 

You can also search with your package name like below:

find . -type f -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep com/mypackage/Hello.class &&  echo {}' 
like image 38
Wenbing Li Avatar answered Oct 13 '22 00:10

Wenbing Li


Where are you jar files? Is there a pattern to find where they are?

1. Are they all in one directory?

For example, foo/a/a.jar and foo/b/b.jar are all under the folder foo/, in this case, you could use find with grep:

find foo/ -name "*.jar" | xargs grep Hello.class 

Sure, at least you can search them under the root directory /, but it will be slow.

As @loganaayahee said, you could also use the command locate. locate search the files with an index, so it will be faster. But the command should be:

locate "*.jar" | xargs grep Hello.class 

Since you want to search the content of the jar files.

2. Are the paths stored in an environment variable?

Typically, Java will store the paths to find jar files in an environment variable like CLASS_PATH, I don't know if this is what you want. But if your variable is just like this:CLASS_PATH=/lib:/usr/lib:/bin, which use a : to separate the paths, then you could use this commend to search the class:

for P in `echo $CLASS_PATH | sed 's/:/ /g'`; do grep Hello.calss $P/*.jar; done 
like image 131
Bin Wang Avatar answered Oct 13 '22 01:10

Bin Wang