0

jar からクラス ファイルを抽出し (jar にある場合)、宛先フォルダーに保存します。

これは私が今持っているものです:

Class Myclass = Thread.currentThread().getContextClassLoader().loadClass(className);
String pathJar = classs.getProtectionDomain().getCodeSource().getLocation().getPath();

pathJar は、クラスを含む jar へのパスを保持します。

さて、そこからクラスを抽出するにはどうすればよいですか?

4

3 に答える 3

0

クラスパスで使用可能な場合、すべての内部クラスを使用してクラスをメモリから直接抽出できるはずです。少なくとも 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()));
        }
    }
}
于 2016-10-29T08:19:31.103 に答える
0

次のライブ コードは、ivy依存関係レポートの 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");
                                }
                        }
                }
        }
于 2013-01-03T07:38:35.080 に答える
0

要件にASMを活用できるかどうかを確認してください。BCELのように、他にもいくつかのオプションがあります。

于 2013-01-03T07:41:05.407 に答える