To use get information about a resource you can use the following API: ClassPathExplorerFacade.getResourceInfo(Class clazz) which will return ResourceInfo with information on package and archive from which the resource was aquired. For example, you might not know from which location a certain resource is loaded from. This could happen, for example, when you need to find out were a compiled JSP is residing; or you project might have dublicate resources and you would not know which archive the resource is read from; or you might have imported the resource and you are wondering from which archive it was loaded. To find such information you can use following code snipplet:
import org.naftulin.classpathexplorer.ClassPathExplorerFacade; import org.naftulin.classpathexplorer.resourceinfo.impl.ResourceInfo; ..... System.out.println("The resource information is: " + ClassPathExplorerFacade.getResourceInfo(resourceInstance.getClass())); ........
To get a stack trace with information on where each reported resource is coming from use the following API: ClassPathExplorerFacade.getExceptionDecorator(Exception e) which will return a sub-class of Exception . Most of the times you will might find this useful when the code throws an exception and you want to de-compile resource to see why the exception is throw but you don't know which archives these resources are located in. Another useful example is when you have a enterprise archive deployed and resources are not loaded as you have expected( class not found exception thrown). Here is an example code snipplet:
import org.naftulin.classpathexplorer.ClassPathExplorerFacade; ..... catch(Exception e) { Exception decorated = ClassPathExplorerFacade.getExceptionDecorator(e); System.out.println(decorated.printStackTrace()); }
To find dublicate resources in you class path you can use the following APIs: ClassPathExplorerFacade.getAllDublicateResources() which would return AccessibleResource or ClassPathExplorerFacade.getAllDublicateResourcesAsStringReport() for a report of the dublicate resources or ClassPathExplorerFacade.getAllDublicateResourcesAsXmlReport() for a XML based report of the dublicate resources. By default, files with all extensions qualify for the dulicate report, and the report will only look at the classpath as defined by the system varaible classpath (or to be exact the System.getProperty("java.class.path") ). ClassPathExplorerFacade contains API that allow to you to specify both the extensions of the files that should be considered for the dublicate report and the path/archives that should be explored. Here is an example code snipplet to use the default string report:
import org.naftulin.classpathexplorer.ClassPathExplorerFacade; ..... System.out.println("The resource information is: " + ClassPathExplorerFacade.getAllDublicateResourcesAsStringReport()); ........
To find dublicate resources, ant task could also be used. Here are the parameters for the ant task (implemented by DublicateResourceDetectorAntTask) class and their meaning:
parameter | description |
path | The path to be considered for dublicate resource scanning. You can either use the path or regular path structure. Either path or path structure is required. |
acceptedExtensions | File extensions to be considered for the dublicate resource scanning. Not required. By default scanning all files. Valid values are list of extensions separated by comma, like ".class, .xml, .properties" |
numDublicateThreshold | a number. If more dublicate resources are found an error would be triggered (unless suppressed). Not required. By default is 0. |
failOnError | Boolean flag. Indicates whether to stop the build if number of dublicate resources exceed the threshold. Not required. By default is true. |
<taskdef name="pathExplorer" classname="org.naftulin.classpathexplorer.DublicateResourceDetectorAntTask" classpath="..place where this jaf file resides .." />Here are a few examples of ant targets:
<!-- find all dublicate resources in the class path. If there are less or equals to 7 dublicates, don't consider it as an error, and don't stop the build if there are more than 7 dublicates --> <pathExplorer path="${java.class.path}" failOnError="false" numDublicateThreshold="7" /> <!-- find all dublicate resources in the class path. Don't stop the build if there are more than any dublicates --> <pathExplorer failOnError="false"> <path> <pathelement path="${java.class.path}" /> </path> </pathExplorer> <!-- find all dublicate resources in the class path and in and all jar files in mydevelopment directory, with all sub-directories. Consider only class files and xml files Stop the build if there are any dublicates --> <pathExplorer acceptedExtensions=".class, *.xml"> <path> <pathelement path="${java.class.path}" /> <fileset dir="C:/mydevelopment"> <include name="**/*.jar" /> </fileset> </path> </pathExplorer>