Category:Hills Have Eyes (film series)
Category:The Hills Have EyesQ:
Java get source code of a jar file
I need to write a Java application that dynamically loads a jar file (for example, with a URLClassLoader) and get the java source code of classes present in the jar file. How can I do this?
A:
You can get the contents of a Jar file using the Jar file API.
The following program shows how you can get the content of all the jars in a directory:
import java.io.IOException;
import java.io.File;
import java.net.URLClassLoader;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarFile;
public class getjars {
public static void main(String[] args) throws IOException {
JarFile jar = null;
String res = "";
Enumeration jar_entries;
// Get all the jars in the current directory
File dir = new File(".");
jar = new JarFile(dir);
jar_entries = jar.entries();
while (jar_entries.hasMoreElements()) {
URLClassLoader ucl = new URLClassLoader(new URL[]{jar_entries.nextElement()});
Class clazz = ucl.loadClass(res);
if(clazz!= null) {
Class c = clazz.getClassLoader();
while(c!= null){
String clazz_name = c.getName(); ac619d1d87
Related links:
Comentários