Monday, January 23, 2006

For Searching the existence of a Class file in .Jar & .War Files

The following program searches for the exitence of a class file in the Jar and War files located under given folder and subfolders.

-------
package JarFileSearch;

import java.io.File;
import java.io.IOException;

import java.util.Enumeration;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


class ClassFileSearch {
Vector objJarFilesVector;
String strFolderName;
String strClassName;
Vector objVector;

ClassFileSearch(String strFolderName, String strClassName) {
objJarFilesVector = new Vector();
this.strFolderName = strFolderName;
this.strClassName = strClassName;
this.objVector = new Vector();
}

public static void main(String[] args) {
String strInFolderAndSubFolders = "C:/srikanth";
String strClassName = "DateUtility.class";

long l_startTime = System.currentTimeMillis();
ClassFileSearch objClassFileSearch = new ClassFileSearch(strInFolderAndSubFolders,
strClassName);
System.out.println("searching for " + strClassName +
" file in .jar and .war files available under " +
strInFolderAndSubFolders);
objClassFileSearch.findAllJarFiles(objClassFileSearch.getFile());
objClassFileSearch.findExitenceOfClass();

long l_endTime = System.currentTimeMillis();
System.out.println("Time Taken (in millis)" +
(l_endTime - l_startTime));
}

public File getFile() {
return new File(this.strFolderName);
}

public void findExitenceOfClass() {
FindRun[] objFindRun = new FindRun[objJarFilesVector.size()];

for (int i = 0; i < objJarFilesVector.size(); i++) {
objFindRun[i] = new FindRun(strClassName,
(String) objJarFilesVector.get(i));
objFindRun[i].start();

//System.out.println("searching --> " + (String)objJarFilesVector.get(i));
try {
objFindRun[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void findAllJarFiles(File objFile) {
File[] objFileArray = objFile.listFiles();

for (int i = 0; i < objFileArray.length; i++) {
if (objFileArray[i].isDirectory()) {
findAllJarFiles(objFileArray[i]);
} else {
String[] strArray = objFileArray[i].getName().split("\\.");

if ((strArray != null) && (strArray.length > 0)) {
if (strArray[strArray.length - 1].equalsIgnoreCase("jar") ||
strArray[strArray.length - 1].equalsIgnoreCase(
"war")) {
objJarFilesVector.add(objFileArray[i].getAbsolutePath());

//objVector.add(new Thread(new FindRun(strClassName,objFileArray[i].getAbsolutePath())));
//((Thread)objVector.get(objVector.size()-1)).start();
}
}
}
} // end of: for
}
} // end of : ClassFileSearch class


class FindRun extends Thread {
String strClassName;
String strJarName;

FindRun(String strClassName, String strJarName) {
this.strClassName = strClassName;
this.strJarName = strJarName;
}

public void run() {
try {
JarFile objJarFile = new JarFile(new File((String) strJarName));
Enumeration objEnumeration = objJarFile.entries();

while (objEnumeration.hasMoreElements()) {
JarEntry objJarEntry = (JarEntry) objEnumeration.nextElement();

if (objJarEntry.getName().indexOf(strClassName) > 0) {
System.out.println(strJarName + " * " +
objJarEntry.getName());
}
}

//System.out.println("searching ..." + strJarName);
} catch (IOException eIOException) {
eIOException.printStackTrace();
}
}
} // end of : FindRun Class
------

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]