簡體   English   中英

按類名從類路徑中的jar中提取類文件

[英]Extracting class file from jar in classpath by classname

我想從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");
                                }
                        }
                }
        }

檢查是否可以利用ASM滿足您的要求。 還有其他幾種選擇,例如BCEL

當類路徑中有可用的所有內部類時,您應該能夠直接從內存中提取您的類。 至少在Oracle 1.8 JVM上,內部類將用$分隔。

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()));
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM