mardi 7 avril 2015

Java: Get classes and type info from Jar File

I have a number of jar files and I want to get the list of classes and interfaces in it as well as additional type information. I'm trying to achieve something like reflection in .net.


Is this possible in java? Is so, how do I do it?


I followed some tutorials online to do this with the Azure mobile services jar file. I was able to get the list of classes but nothing more. When I try to create the class, it complains about some dependencies not found even though they are in the same folder.



package pkg;

import java.io.FileInputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import java.util.ArrayList;
import java.net.URL;
import java.net.URLClassLoader;

public class Program {
public static void main(String[] args) {
String[] files = {"U:\\Temp\\mobileservices - Copy\\gson-2.2.2.jar" , "U:\\Temp\\mobileservices - Copy\\mobileservices-1.1.5.jar"};
String[] target = {"U:\\Temp\\mobileservices - Copy\\mobileservices-1.1.5.jar"};
//load the classes specifying the one needed
Extract extract = ClassFinder.extractClassDetails(files, target);
//write the failed
System.out.println("Failed classes");
for(String className: extract.failed){
System.out.println(className);
}
}
}

class ClassFinder{
public static ParseInfo getClassNames(String jarFilename){
ParseInfo output = new ParseInfo();
output.classes = new ArrayList<String>();
output.filename = jarFilename;
try{
JarInputStream jarFile = new JarInputStream(new FileInputStream(jarFilename));
JarEntry jar;

while(true){
jar = jarFile.getNextJarEntry();
if (jar==null){
break;
}
if (jar.getName().endsWith(".class")){
String className = jar.getName().replaceAll("/", "\\.");
String myClass = className.substring(0, className.lastIndexOf("."));
output.classes.add(myClass);
}
}
} catch (Exception e){
System.out.println("Oops... Encounter an issue while parsing jar " + e.toString());
}
return output;
}
public static Extract extractClassDetails(String[] files, String[] target){
Extract output = new Extract();
//extract the filenames
URL[] urls = new URL[files.length];
int i = 0;
for(String file: files){
System.out.println("Adding jar file: "+file);
try{
urls[i++] = new URL("jar:file:" + file + "!/");
}catch(Exception e){}
}
//create the loader
URLClassLoader classLoader = URLClassLoader.newInstance(urls);
//create the classes for the target
for(String file: target){
ParseInfo info = getClassNames(file);
//todo: walk through the classes
System.out.println("Parsed "+file);
for(String className: info.classes){
System.out.print("..");
try{
Class c = classLoader.loadClass(className);
int a = 20;
}catch(Exception e){
System.out.println("Ei. Error oh! "+e.toString());
output.failed.add(className);
}
System.out.print(".");
}
System.out.println("Done creating classes");
}

return output;
}
}

class ParseInfo {
public String filename;
public ArrayList<String> classes;
}
class Extract{
public ArrayList<String> failed;
}





Aucun commentaire:

Enregistrer un commentaire