Search This Blog

Thursday, February 19, 2009

Resolving ClassNotFound Exceptions

When you are running a Java program or compiling a java program you got a class not found error, going through all the JAR files for finding this particular class file is quite tedious.

The program given below can help you to search inside the jar and/or zip files and tells you whether the file you are looking for is present or not.

Code Example

Below you will find the code example, how you use it and what result you can expect out of it.

SearchJars.java
  package mypackage7;

import java.util.zip.*;
import java.io.*;
import java.util.*;

public class SearchJars {

static String s3;
static String s4 ="";
static int i1 = 0;
static int i2 = 0;

public static void main(String args[]) {
if (args.length != 2) throw new IllegalArgumentException("Wrong Number of Args !! java SearchJars Direcory_Name filename ");
File f1 = new File(args[0]);
s3 = args[1];
traverse(f1);
System.out.println("\n\n============== Results ============================");
System.out.println("Searced "+ i1 +" Files in "+ args[0] +" And Found "+i2 +" Entries. ");
System.out.println(s4);
}


public static void traverse(File f) {
String s1 = f.getAbsolutePath();

if (s1.toUpperCase().indexOf(".JAR") != -1 || s1.toUpperCase().indexOf(".ZIP") != -1 ) {
System.out.println("Searching "+ s1 +" .....");
i1++;
try {
ZipFile zf = new ZipFile(s1);
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
String s2 = ((ZipEntry)entries.nextElement()).getName();
if (s2.indexOf(s3) != -1 ) {
System.out.println("Found "+s2 + " In " +s1);
i2++;
s4 = s4 + "Found "+s2 + " In " +s1 + "\n";
}
}
} catch (IOException e) {}
}

if (f.isDirectory()) {
String[] children = f.list();
for (int i=0; i<children.length; i++) {
traverse(new File(f, children[i]));
}
}
}
}


Program Usage

java.exe mypackage7.SearchJars D:\oracle\jdev1012\BC4J\jlib DateConvertor

Program Output


  Searching D:\oracle\jdev1012\BC4J\jlib\adfjclient.jar  ..... 
Found oracle/jbo/uicli/jui/DateConvertor.class In D:\oracle\jdev1012\BC4J\jlib\adfjclient.jar
Searching D:\oracle\jdev1012\BC4J\jlib\adfmejb.jar .....
Searching D:\oracle\jdev1012\BC4J\jlib\adftags.jar .....
Searching D:\oracle\jdev1012\BC4J\jlib\bc4jdatum.jar .....

============== Results ============================
Searced 4 Files in D:\oracle\jdev1012\BC4J\jlib And Found 1 Entries.
Found oracle/jbo/uicli/jui/DateConvertor.class In D:\oracle\jdev1012\BC4J\jlib\adfjclient.jar

No comments: