Java 按类名从类路径中的jar中提取类文件

Java 按类名从类路径中的jar中提取类文件,java,class,jar,Java,Class,Jar,我想从jar中提取一个类文件(如果它在jar中)并将其保存在目标文件夹中 这就是我现在拥有的: Class Myclass = Thread.currentThread().getContextClassLoader().loadClass(className); String pathJar = classs.getProtectionDomain().getCodeSource().getLocation().getPath(); pathJar保存类的jar路径 现在,如何从中提取类?下

我想从jar中提取一个类文件(如果它在jar中)并将其保存在目标文件夹中

这就是我现在拥有的:

Class Myclass = Thread.currentThread().getContextClassLoader().loadClass(className);
String pathJar = classs.getProtectionDomain().getCodeSource().getLocation().getPath();
pathJar保存类的jar路径


现在,如何从中提取类?

下面的代码是实时的,它通过依赖项报告XML文件进行解析,并从中的JAR中提取所有文件,我希望您会觉得有用:

 private void extract(String path2, String outPath) {
                JarFile zf = null;
                try {
                        zf = new JarFile(path2);
                } catch (IOException e4) {
                        // according to http://stackoverflow.com/a/470492/783412, set the
                        // logger to log these events
                        System.setProperty("java.util.logging.ConsoleHandler.level",
                                        "FINER");
                        logger.throwing(
                                        Thread.currentThread().getStackTrace()[1].getClassName(),
                                        Thread.currentThread().getStackTrace()[1].getMethodName(),
                                        e4);

                        // according to
                        // http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
                        // the default logging level is INFO
                        System.setProperty("java.util.logging.ConsoleHandler.level", "INFO");
                } 
                if (zf == null) {
                        System.err.println(path2 + " is an invalid jar, skipping");
                        return;
                }
                Enumeration<JarEntry> ze = (Enumeration<JarEntry>) zf.entries();
                while (ze.hasMoreElements()) {
                        JarEntry thisEntry = ze.nextElement();
                        System.err.println(thisEntry.getName());
                        if (thisEntry.isDirectory()) {
                                new File(outPath + File.separator + thisEntry.getName())
                                                .mkdirs();
                        } else {
                                String filename = thisEntry.getName();
                                if (filename.endsWith("RSA") || filename.endsWith("DSA") || filename.endsWith("SF")) continue;
                                FileOutputStream fos = null;
                                try {
                                        fos = new FileOutputStream(outPath + File.separator
                                                        + filename);
                                } catch (FileNotFoundException e3) {
                                        // according to http://stackoverflow.com/a/470492/783412,
                                        // set the logger to log these events
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "FINER");
                                        logger.throwing(Thread.currentThread().getStackTrace()[1]
                                                        .getClassName(), Thread.currentThread()
                                                        .getStackTrace()[1].getMethodName(), e3);

                                        // according to
                                        // http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
                                        // the default logging level is INFO
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "INFO");
                                }
                                BufferedInputStream zis = null;
                                try {
                                        zis = new BufferedInputStream(zf.getInputStream(thisEntry));
                                } catch (IOException e2) {
                                        // according to http://stackoverflow.com/a/470492/783412,
                                        // set the logger to log these events
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "FINER");
                                        logger.throwing(Thread.currentThread().getStackTrace()[1]
                                                        .getClassName(), Thread.currentThread()
                                                        .getStackTrace()[1].getMethodName(), e2);

                                        // according to
                                        // http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
                                        // the default logging level is INFO
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "INFO");
                                }
                                try {
                                        fos = new FileOutputStream(outPath + File.separator
                                                        + thisEntry.getName());
                                } catch (FileNotFoundException e1) {
                                        // according to http://stackoverflow.com/a/470492/783412,
                                        // set the logger to log these events
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "FINER");
                                        logger.throwing(Thread.currentThread().getStackTrace()[1]
                                                        .getClassName(), Thread.currentThread()
                                                        .getStackTrace()[1].getMethodName(), e1);

                                        // according to
                                        // http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
                                        // the default logging level is INFO
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "INFO");
                                }
                                byte[] b = new byte[new Long(thisEntry.getSize()).intValue()];
                                try {
                                        zis.read(b);
                                } catch (IOException e1) {
                                        // according to http://stackoverflow.com/a/470492/783412, set the logger to log these events
                                        System.setProperty("java.util.logging.ConsoleHandler.level","FINER");
                                        logger.throwing(Thread.currentThread().getStackTrace()[1].getClassName(), Thread.currentThread().getStackTrace()[1].getMethodName(), e1);

                                        // according to http://stackoverflow.com/questions/4970513/java-logging-levels-confusion, the default logging level is INFO
                                        System.setProperty("java.util.logging.ConsoleHandler.level", "INFO");           
                                }
                                try {
                                                        .getClassName(), Thread.currentThread()
                                                        .getStackTrace()[1].getMethodName(), e1);

                                        // according to
                                        // http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
                                        // the default logging level is INFO
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "INFO");
                                }
                                byte[] b = new byte[new Long(thisEntry.getSize()).intValue()];
                                try {
                                        zis.read(b);
                                } catch (IOException e1) {
                                        // according to http://stackoverflow.com/a/470492/783412, set the logger to log these events
                                        System.setProperty("java.util.logging.ConsoleHandler.level","FINER");
                                        logger.throwing(Thread.currentThread().getStackTrace()[1].getClassName(), Thread.currentThread().getStackTrace()[1].getMethodName(), e1);

                                        // according to http://stackoverflow.com/questions/4970513/java-logging-levels-confusion, the default logging level is INFO
                                        System.setProperty("java.util.logging.ConsoleHandler.level", "INFO");           
                                }
                                try {
                                        fos.write(b);
                                } catch (IOException e1) {
                                        // according to http://stackoverflow.com/a/470492/783412, set the logger to log these events
                                        System.setProperty("java.util.logging.ConsoleHandler.level","FINER");
                                        logger.throwing(Thread.currentThread().getStackTrace()[1].getClassName(), Thread.currentThread().getStackTrace()[1].getMethodName(), e1);
                                        // according to http://stackoverflow.com/questions/4970513/java-logging-levels-confusion, the default logging level is INFO
                                        System.setProperty("java.util.logging.ConsoleHandler.level", "INFO");           
                                } 
                                try {
                                        fos.close();
                                } catch (IOException e) {
                                        // according to http://stackoverflow.com/a/470492/783412,
                                        // set the logger to log these events
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "FINER");
                                        logger.throwing(Thread.currentThread().getStackTrace()[1]
                                                        .getClassName(), Thread.currentThread()
                                                        .getStackTrace()[1].getMethodName(), e);

                                        // according to
                                        // http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
                                        // the default logging level is INFO
                                        System.setProperty(
                                                        "java.util.logging.ConsoleHandler.level", "INFO");
                                }
                        }
                }
        }
private void extract(字符串路径2,字符串输出路径){
JarFile zf=null;
试一试{
zf=新的JarFile(路径2);
}捕获(IOE4异常){
//据http://stackoverflow.com/a/470492/783412,设置
//日志记录器来记录这些事件
System.setProperty(“java.util.logging.ConsoleHandler.level”,
“更精细”);
投掷(
Thread.currentThread().getStackTrace()[1].getClassName(),
Thread.currentThread().getStackTrace()[1].getMethodName(),
e4);
//据
// http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
//默认日志记录级别为INFO
set属性(“java.util.logging.ConsoleHandler.level”、“INFO”);
} 
如果(zf==null){
System.err.println(path2+“是无效的jar,正在跳过”);
返回;
}
枚举ze=(枚举)zf.entries();
while(ze.hasMoreElements()){
JarEntry thisEntry=ze.nextElement();
System.err.println(thisEntry.getName());
if(thisEntry.isDirectory()){
新文件(outPath+File.separator+thisEntry.getName())
.mkdirs();
}否则{
字符串文件名=thisEntry.getName();
如果(filename.endsWith(“RSA”)| filename.endsWith(“DSA”)| filename.endsWith(“SF”)继续;
FileOutputStream=null;
试一试{
fos=新文件输出流(输出路径+文件分隔符
+文件名);
}捕获(FileNotFoundException e3){
//据http://stackoverflow.com/a/470492/783412,
//将记录器设置为记录这些事件
System.setProperty(
“java.util.logging.ConsoleHandler.level”、“FINER”);
logger.throwing(Thread.currentThread().getStackTrace()[1]
.getClassName(),Thread.currentThread()
.getStackTrace()[1].getMethodName(),e3);
//据
// http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
//默认日志记录级别为INFO
System.setProperty(
“java.util.logging.ConsoleHandler.level”、“INFO”);
}
BufferedInputStream zis=null;
试一试{
zis=新的BufferedInputStream(zf.getInputStream(thisEntry));
}捕获(IOE2异常){
//据http://stackoverflow.com/a/470492/783412,
//将记录器设置为记录这些事件
System.setProperty(
“java.util.logging.ConsoleHandler.level”、“FINER”);
logger.throwing(Thread.currentThread().getStackTrace()[1]
.getClassName(),Thread.currentThread()
.getStackTrace()[1].getMethodName(),e2);
//据
// http://stackoverflow.com/questions/4970513/java-logging-levels-confusion,
//默认日志记录级别为INFO
System.setProperty(
“java.util.logging.ConsoleHandler.level”、“INFO”);
}
试一试{
fos=新文件输出流(输出路径+文件分隔符
+thisEntry.getName());
}捕获(FileNotFoundException e1){
//据http://stackoverflow.com/a/470492/783412,
//将记录器设置为记录这些事件
System.setProperty(
“java.util.logging.ConsoleHandler.level”、“FINER”);
logger.throwing(Thread.currentThread().getStackTrace()[1]
.getClassName(),Thread.currentThread()
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ExtractClass {
    public static void main(String... args) throws ClassNotFoundException, IOException {
        final String name = args[0];

        extractClass(name, Paths.get(name));
    }

    private static void extractClass(final String name, final Path path) throws IOException, ClassNotFoundException {
        final Class<?> clazz = Class.forName(name);

        try (InputStream inputStream = Object.class.getResourceAsStream("/" + name.replace(".", "/") + ".class")) {
            Files.copy(inputStream, Paths.get(name));
        }

        // Retrieve all inner classes
        for (final Class<?> innerClass : clazz.getDeclaredClasses()) {
            extractClass(innerClass.getName(), Paths.get(innerClass.getName()));
        }
    }
}