Java Class Finder

thekashyap 0 Tallied Votes 206 Views Share

Hi *,
Here is a small program I had created to search for a given .class file within given CLASSPATH.
It is primarily used in our project on test machines where lotsa ppl put lotsa patches in lotsa paths/jars and finally end up wondering which class file is being used.
This tool a tool searches for a given class file in given classpath and it:
- Gives path where the file was found first.
- Gives a list of paths in which file was found.
- Validates all the paths put in your CLASSPATH.
- Works on Unix and Windows.
E.g. :

smlcad@(om2_kash_src)> java FindClass
Usage:
java -cp <classpath-to-search> FindClass <class-to-search>

E.g.
java -cp $CLASSPATH FindClass ca.siemens.tic.smlc.lt.api.rtp.Log
OR
execRTPenv java FindClass ca.siemens.tic.smlc.lt.api.rtp.Log

smlcad@(om2_kash_src)> java FindClass ca.siemens.tic.smlc.lt.api.rtp.Log

Trying to search ca.siemens.tic.smlc.lt.api.rtp.Log


Searching in 1: "." *****
Searching in 2: "/vobs/projects/smlc_src/rtp/java/classes" *****
Searching in 3: "/vobs/devtools/junit3.8.1/junit.jar"
Searching in 4: "/opt/iona"
Searching in 5: "/opt/iona/lib/art/omg/5/omg.jar"
Searching in 6: "/opt/smlc/lib/fwk_intf.jar"
Searching in 7: "/etc/opt/iona/domains/orbixsmlcpt3"
Searching in 8: "/opt/SMAW/SMAWrtpap/libexec/ApacheJServ.jar"
Searching in 9: "/opt/SMAW/SMAWrtpap/libexec/gnujsp10.jar"
Searching in 10: "/opt/SMAW/SMAWrtpap/libexec/servlet-2.0-plus.jar"
Searching in 11: "/opt/SMAW/SMAWrtp/lib/RtpAdmServer.jar"
Searching in 12: "/opt/SMAW/SMAWrtp/lib/RtpAdmGuiJspUtil.jar"
Searching in 13: "/opt/SMAW/SMAWrtp/lib/RtpAdmAppExt.jar" *****
Searching in 14: "/opt/SMAW/SMAWrtp/lib/RtpAdmAppFallback.jar"
Searching in 15: "/opt/SMAW/SMAWrtp/lib/gnu-regexp.jar"
Searching in 16: "/opt/SMAW/SMAWrtp/lib/RtpAudMgr.jar"
Searching in 17: "/opt/SMAW/SMAWrtp/lib/RtpAudRecUtil.jar"
Searching in 18: "/opt/SMAW/SMAWrtp/lib/RtpRecMgr.jar"
Searching in 19: "/opt/SMAW/SMAWrtp/lib/RtpCleanupUpd.jar"
Searching in 20: "/opt/SMAW/SMAWrtp/lib/RtpTcnShow.jar"
Searching in 21: "/opt/SMAW/SMAWrtp/lib/jce1_2_1.jar"
Searching in 22: "/opt/SMAW/SMAWrtp/lib/sunjce_provider.jar"
Searching in 23: "/opt/SMAW/SMAWrtpjr/lib/tools.jar"
Searching in 24: "/export/home/oracle/products/9.2.0/jdbc/lib/classes111.zip"
Searching in 25: "/opt/SMAW/SMAWrtp/lib/RtpAdmAppExt.jar" *****


Results:

Following paths do not exist OR are not readable !
5: "/opt/iona/lib/art/omg/5/omg.jar"
6: "/opt/smlc/lib/fwk_intf.jar"
7: "/etc/opt/iona/domains/orbixsmlcpt3"
19: "/opt/SMAW/SMAWrtp/lib/RtpCleanupUpd.jar"
21: "/opt/SMAW/SMAWrtp/lib/jce1_2_1.jar"
22: "/opt/SMAW/SMAWrtp/lib/sunjce_provider.jar"
23: "/opt/SMAW/SMAWrtpjr/lib/tools.jar"

First found in: 1: "./"
Also found in following 2 path(s):
2: "/vobs/projects/smlc_src/rtp/java/classes/"
13: "/opt/SMAW/SMAWrtp/lib/RtpAdmAppExt.jar"
25: "/opt/SMAW/SMAWrtp/lib/RtpAdmAppExt.jar"


smlcad@(om2_kash_src)>

Known problem:
Doesn't work with ALL versions of Java. If you test of future versions please do comment and let other know the results.

import java.io.File;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.StringTokenizer;
import java.util.Vector;

public class FindClass
{
    public static void main(String[] args) throws Exception {

        String javaVer = System.getProperty("java.version") ;
        if( javaVer.startsWith("1.4") ) {
            log( "\n\nSorry somehow this program doesn't work with Java 1.4XXX !\nIf you're using \n\"execRTPenv java FindClass...\"\nthen you can try\n\"execRTPenv /vobs/devtools/java_sdk/bin/java FindClass...\".\n\n" ) ;
            System.exit(0);
        }
        String classToFind = null ;

        if( 0 == args.length ){
            log("Usage:\n\tjava -cp <classpath-to-search> FindClass <class-to-search>\n\n\tE.g.\n\t\tjava -cp $CLASSPATH FindClass ca.siemens.tic.smlc.lt.api.rtp.Log\n\n") ;
            System.exit(1) ;
        }
        else
            classToFind = args[0] ;

        log("\nTrying to search " + classToFind ) ;
        log("\n") ;

        try {
            StringTokenizer st =
                new StringTokenizer(System.getProperty("java.class.path"), System.getProperty("path.separator") ) ;
            int totalPaths = st.countTokens() ;
            Vector foundPaths = new Vector() ;
            Vector erroredPaths = new Vector() ;
            String thisPath = null ;
            for( int i = 0; i < totalPaths; i++ )
            {
                thisPath = st.nextToken() ;
                System.out.print( "Searching in " + Integer.toString(i+1) + ": \"" + thisPath + "\"") ;
                File thisPathFile = new File(thisPath) ;

                String checkStr = "OK" ;
                if( !thisPathFile.exists() || !thisPathFile.canRead() ) {
                    erroredPaths.add(Integer.toString(i+1) + ": \"" + thisPath + "\"") ;
                    log( "" ) ;
                    continue ;
                }

                if( thisPathFile.isDirectory() && thisPath.charAt(thisPath.length()-1) != '/' ) {
                    thisPath = new String( thisPath + '/' ) ;
                }
                else if ( thisPathFile.isFile() && thisPath.charAt(thisPath.length()-1) == '/' ) {
                    thisPath = thisPath.substring( 0, thisPath.length()-1 ) ;
                }

                URL thisURL = new URL("file", InetAddress.getLocalHost().getHostName(), thisPath.trim() ) ;

                URLClassLoader ul = new URLClassLoader( new URL[] { thisURL }, null ) ;
                //log( ul.getURLs() ) ;
                try {
                    ul.loadClass(classToFind) ;
                    //log("-->" + thisPath) ;
                    log( " *****" ) ;
                    foundPaths.add( Integer.toString(i+1) + ": \"" + thisPath + "\"" ) ;
                }
                catch( ClassNotFoundException e ) { log( "" ) ; }
            }
            log("\n\nResults:\n") ;
            if(erroredPaths.size() > 0 ) {
                log( "Following paths do not exist OR are not readable !" ) ;
                for(int j = 1; j < erroredPaths.size(); j++ )
                    log( "\t" + erroredPaths.get(j).toString() ) ;
            }
            if( foundPaths.size() == 0 )
            {
                log( "\nSorry Couldn't find given class in given CLASSPATH." ) ;
            }
            else
            {
                if( foundPaths.size() > 1 )
                {
                    log( "\nFirst found in: " + foundPaths.get(0) ) ;
                    log( "Also found in following " + Integer.toString(foundPaths.size()-1) + " path(s): " ) ;
                    for(int j = 1; j < foundPaths.size(); j++ )
                    {
                        log( "\t" + foundPaths.get(j).toString() ) ;
                    }
                }
                else
                    log("\nFound only in: " + foundPaths.get(0) ) ;
            }
            log("\n") ;
        } catch( Exception e ) {
            log( e ) ;
        }
    }

    public  FindClass() {

    }

    public static void log( Throwable e ){ System.out.println( e.getMessage() ) ; e.printStackTrace() ; }
    public static void log( String s ){ System.out.println( s ) ; }
    public static void log( URL[] s ){
        for(int i = 0 ; i< s.length; i++ )
            System.out.println( s[i] ) ;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.