Skip to main content
Last updated March 07, 2012 16:49, by larsfiedler
Feedicon  

Introduction

Did you ever have a ClassNotFoundException and you did not know what jar-file is missing? And you were clicking through a hundred jar-files to find that class? Then jarscan helps you. jarscan searches recursively all archives in the current directory and all subdirectories for a certain entry. It even recursively scans archives in archives. You can also search for text in files, for the JDK-Version of .class files or for doubled files in archives.

You can use jarscan as a command line tool or as a library.

1. Using jarscan as a command line tool

1.1. Quick start

The dowloaded jarscan.zip contains the executable jar file "jarscan.jar" and "jarscan.bat" to start it. Edit jarscan.bat and start it to see the usage.

1.2. Installation

  • Make sure you have installed JRE1.5 or higher
  • Unzip the downloaded "jarscan.zip"
  • Copy the files "jarscan.bat" and "jarscan.jar" in a directory of your choice , e.g. C:\tools
  • If you choose another directory you have to edit jarscan.bat, e.g.: java -jar C:\xyz\jarscan.jar %*
  • Set the PATH-variable to that directory: set PATH=%PATH%;C:\tools
  • Open a console-window and change to a directory that contains archives.


1.3. Usage

1.3.1. Filename

To search for the entry "text" in all archives of the current directory and all subdirectories type:

 jarscan text

Read the output:

  • ...: The first points show you the progress of scanning. Every point represents a scanned archive.
  • Output: The current directory contains "archive.zip" that contains "textFile1.txt". The subdirectory "dir2" contains "archive2.jar" that contains "textInArchive2.txt" and so on.
  • Suffix: Note that jarscan does not look at the suffix. For example if you rename an archive to archive.txt jarscan will also scan it.
  • Wildcards: jarscan always uses wildcards. So if you search for text, atextb would be a hit.
  • Case Sensitive: Searching is case sensitive. So if you search for text, aText would not be a hit


1.3.2. Directory

To start scanning from a different directory, type "-d":

 jarscan -d testjars text 

Read the output:

  • jarscan started scanning in the directory 'testjars'.


1.3.3. JavaMode

To convert all "." to "/" in the searchString type "-j". This feature is useful if you have a ClassNotfoundException and you just want to copy the error message to the console:

 jarscan -j javax.persistence.Table 

Read the output:

  • because of the "-j" option jarscan searched for javax/persistence/Table


1.3.3. Recursion:

To recursively scan archives in archives, type:

 jarscan -r text

Read the output:

  • As you see "archive3.zip" contains "archive4.jar", and "archive4.jar" contains "archive5.jar". "archive5.jar" contains a text file "textInArchive5.txt".


1.3.4. File content

To scan files for a certain String, type:

 jarscan -f bla

Read the output:

  • "archive.zip" contains the text file "textFile1.txt". This text file contains the line "Stupid Text: blabla".
  • The file "textFile.txt" exists also outside of "archive.zip" and it has also a line "Stupid Text: blabla".
  • Note that only the first line that contains "bla" is printed. If you want to see all lines, use -v.
  • If the line is longer than 100 plus the length of searchString, the line will be cut.
  • Note that you can use -f and -r together.
  • Note by default all files are scanned except the ones with the suffices: ".exe", ".bmp", ".jpg", ".jpeg", ".gif", ".png" and all archives. You can exclude other files from scanning with -e. It is a good idea to edit "jarscan.bat" to add some suffices that should always be excluded.
  • If your searchString contains spaces, use "" (e.g.: jarscan -f "throws Exception")


1.3.5. Manifest

To only scan the manifest-files in archives, type:

 jarscan -m Test

Read the output:

  • archive.zip has a manifest-file with an Entry "Test-Version: 1.1".
  • Note that all keys and values are compared with the searchString.


1.3.6. Compiler version

To check the compiler version the class files were compiled with, use the -c option:

 jarscan -r -c 1.5 

Read the output:

  • jarscan has found 2 class files that were compiled with JDK 1.5. One is inside the nested archive named „archive5.jar“. The other is not in any archive.
  • Note that jarscan stops scanning an archive when it has found one class file with the specified version. If you want to see all class files per archive, use the -v option.
  • You can also scan a range by using „<“ or „>“. E.g.: jarscan -c "<1.6" finds all .class files that are compiled with JDK1.5 or earlier.
  • Note that you must escape „<“ or „>“ because of their meaning for the operating system.


1.3.7. Doubles

To search for doubled classes in different archives, use the -double option:

 jarscan -double

Read the output:

  • archive.zip contains FileScan.class and also archive2.jar contains FileScan.class.
  • Note that only one doubled file per archive is printed. If you want to print all doubled files per archive, use the -v option.


1.3.8. Verbose

You can use -v together with the -f, -c or -double options to get more information. Without the -v option jarscan prints only one file per archive. With the -v option jarscan prints all hits per archive:

 jarscan -v -c 1.5

1.3.9. Exclude

To exclude some files from scanning, type:

 jarscan -e .jar text

Read the output:

  • Jarscan does not scan any archive that ends with ".jar".
  • Note that if you do not use the -e option, jarscan recognizes all archives even if the archive has an unusual suffix. For example, if you rename the archive archive.zip to archive.txt, jarscan will scan archive.txt.


1.3.10. Suffixes

By default jarscan only checks if an archive is damaged, if it has one of the following suffixes: .jar,.zip,.war,.ear, .tar. You can extend the list of suffixes by typing "-s":

 jarscan -s .par text

Read the output:

  • damaged.par is a damaged archive. With the option -s .par jarscan treats damaged.par as an archive and prints the error. Usually jarscan prints only errors for files with one of the following suffixes: ".jar,.zip,.war,.ear, .tar".
  • jarscan only recognizes archives in archives if they have one of the specified suffixes.


2. Using jarscan as a library

jarscan is designed to be also used in java applications. If your application needs to scan jar files for certain entries, use the classes of the library "jarscan.jar". Please read chapter1 to get familiar with the features of jarscan. You can use these features programmatically by using de.dixti.jarscan.JarScan and de.dixti.jarscan.Options.

2.1. Example

 package de.dixti.jarscan.tutorial;
 import de.dixti.jarscan.JarScan;
 import de.dixti.jarscan.Options;
 import de.dixti.jarscan.Result;
 import java.io.File;
 import java.util.List;
 
 public class JarScanTutorial {
 
     public static void main(String[] args) {
          Options options = new Options("text");
          options.setDir(new File("testjars"));
          JarScan jarScan = new JarScan(options);
          jarScan.setPrinter(null);
          Result rootResult = jarScan.scan();
          for (Result result : rootResult.getResultList()) {
              System.out.println(result.getPath());
              for (Result subResult : result.getResultList()) {
                   System.out.println(subResult.getPath());
              }
          }
     }
 }

This prints to the console something like:

 testjars\archive.zip
 testjars\archive.zip\textFile1.txt
 testjars\dir2\archive2.jar
 testjars\dir2\archive2.jar\textInArchive2.txt
 testjars\dir2\dir3\archive3.zip
 testjars\dir2\dir3\archive3.zip\textInArchive3.txt

2.2 Explaining the sample code:

JarScan: JarScan is the class that scans the archives. You can set some options via the the setter-methods of the Options class. In the example the archives are scanned for "text". Scanning starts in the directory "testjars". Recursive is set to false, so archives in archives are not scanned.

rootResult: this is a List of all toplevel results with all scanning information subresults: every result can contain subresults. In this case there are subresults for all files called „*text*“.

For further information read the javadoc that is bundled with the downloaded zip-file.

 
 
Close
loading
Please Confirm
Close