Search code examples
javametrics

How to find the number of files in a jar file


I'm a beginner in java and this is not actually a program question, but I would like to get some advice from you guys on how to get the number of files in a .jar java file? Is there any metric tools that I can use for this (consider its a .jar file)?


Solution

  • As .jar files are just .zip files, you can use the same tools to work with them. On *nix, if you want to list all of the files in a JAR:

    $ unzip -l filename.jar
    

    Will produce output similar to the following:

    Archive:  filename.jar
      Length     Date   Time    Name
     --------    ----   ----    ----
         1211  03-03-14 13:06   bar/foo/swing/window/FrameOperatorFactory.class
         2344  03-03-14 13:06   bar/foo/swing/window/FrameOperator.class
         1509  06-30-07 14:03   LICENSE.txt
     --------                   -------
      5184390                   3 files
    

    (The above output is only an example.)

    If you want just *.class files:

    $ unzip -l filename.jar | grep \.class | wc -l
    

    If you want more control, you're likely better off just unzipping it and working with the files that way.