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

前端 未结 8 690
灰色年华
灰色年华 2021-01-29 23:58

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 t

相关标签:
8条回答
  • 2021-01-30 00:57

    I have used this small snippet. Might be slower but works every time.

    for i in 'find . -type f -name "*.jar"'; do
        jar tvf $i | grep "com.foo.bar.MyClass.clss";
        if [ $? -eq 0 ]; then echo $i; fi;
    done
    
    0 讨论(0)
  • 2021-01-30 00:59

    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
    
    0 讨论(0)
提交回复
热议问题