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

前端 未结 8 677
灰色年华
灰色年华 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:33

    Just go to the directory which contains jars and insert below command:

    find *.jar | xargs grep className.class
    

    Hope this helps!

    0 讨论(0)
  • 2021-01-30 00:33
       locate *.jar | grep Hello.class.jar
    

    The locate command to search the all path of the particular file and display full path of the file.

    example

    locate *.jar | grep classic.jar
    
    /opt/ltsp/i386/usr/share/firefox/chrome/classic.jar
    
    /opt/ltsp/i386/usr/share/thunderbird/chrome/classic.jar
    
    /root/.wine/drive_c/windows/gecko/0.1.0/wine_gecko/chrome/classic.jar
    
    /usr/lib/firefox-3.0.14/chrome/classic.jar
    
    /usr/lib/firefox-3.5.2/chrome/classic.jar
    
    /usr/lib/xulrunner-1.9.1.2/chrome/classic.jar
    
    /usr/share/firefox/chrome/classic.jar
    
    /usr/share/thunderbird/chrome/classic.jar
    
    0 讨论(0)
  • 2021-01-30 00:37

    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 {}'
    
    0 讨论(0)
  • 2021-01-30 00:45

    Linux, Walkthrough to find a class file among many jars.

    Go to the directory that contains the jars underneath.

    eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ ls
    blah.txt                             metrics-core-2.2.0.jar
    jopt-simple-3.2.jar                  scala-library-2.10.1.jar
    kafka_2.10-0.8.1.1-sources.jar       zkclient-0.3.jar
    kafka_2.10-0.8.1.1-sources.jar.asc   zookeeper-3.3.4.jar
    log4j-1.2.15.jar
    

    I'm looking for which jar provides for the Producer class.

    Understand how the for loop works:

    eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ for i in `seq 1 3`; do
    > echo $i
    > done
    1
    2
    3
    

    Understand why find this works:

    eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ find . -name "*.jar"
    ./slf4j-api-1.7.2.jar
    ./zookeeper-3.3.4.jar
    ./kafka_2.10-0.8.1.1-javadoc.jar
    ./slf4j-1.7.7/osgi-over-slf4j-1.7.7-sources.jar
    

    You can pump all the jars underneath into the for loop:

    eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ for i in `find . -name "*.jar"`; do
    > echo $i
    > done
    
    ./slf4j-api-1.7.2.jar
    ./zookeeper-3.3.4.jar
    ./kafka_2.10-0.8.1.1-javadoc.jar
    ./kafka_2.10-0.8.1.1-sources.jar
    

    Now we can operate on each one:

    Do a jar tf on every jar and cram it into blah.txt:

    for i in `find . -name "*.jar"`; do echo $i; jar tf $i; done > blah.txt
    

    Inspect blah.txt, it's a list of all the classes in all the jars. You can search that file for the class you want, then look for the jar that came before it, that's the one you want.

    0 讨论(0)
  • 2021-01-30 00:46

    Use deepgrep and deepfind. On debian-based systems you can install them by:

    sudo apt-get install strigi-utils
    

    Both commands search for nested archives as well. In your case the command would look like:

    find . -name "*.jar" | xargs -I {} deepfind {} | grep Hello.class
    
    0 讨论(0)
  • 2021-01-30 00:53
    eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ for i in `find . -name "*.jar"`; do
    > echo $i
    > done
    
    ./slf4j-api-1.7.2.jar
    ./zookeeper-3.3.4.jar
    ./kafka_2.10-0.8.1.1-javadoc.jar
    ./kafka_2.10-0.8.1.1-sources.jar
    
    0 讨论(0)
提交回复
热议问题