Skip to main content

Find the Source Jar of a Class file in Java

/**
*
* Gets the Source Jar File Location for the given Class,
* Useful to troubleshoot Class Related Exceptions/Errors.
* @param className
* @return
*/
public String getClassSourceJar(String className) {
 Class namedClass = null;
 String classSrcJarLocation = null;
try {
namedClass = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new RuntimeException("The Requested Class is Not Found "+className);
}
CodeSource src = null;
if(namedClass!=null) {
src = namedClass.getProtectionDomain().getCodeSource();
 if(src!=null) {
 URL jar = src.getLocation();
 if(jar!=null) {
 classSrcJarLocation = jar.toString();
 }
 }
}
 return classSrcJarLocation;
}

Comments

Popular posts from this blog

Data Structures using Java

package com.test.count; /**   *     * @author Kiran   *   */ public class LinkedList { Node headNode ; /** *   This method appends the Node at the end of the List *   * @param data */ public void appendNode( int data ) { Node currentNode = headNode ; if ( currentNode == null ) { Node newNode = new Node( data ); headNode = newNode ; return ; } while ( currentNode .getNextNode()!= null ) { currentNode = currentNode .getNextNode(); } currentNode .setNextNode( new Node( data )); } /** * This method prints all the Nodes/Values of the   * LinkedList *   */ public void printNodes() { Node currentNode = headNode ; if ( currentNode == null ) { System. out .println( " The Node is Null" ); return ; } else { System. out .print( "   " + currentNode .getData()); while ( currentNo...