简体   繁体   English

从java中的ClassLoader获取.class文件

[英]get the .class file from ClassLoader in java

I compiled a Class from text 我从文本编译了一个类

public class Foo{
    public Foo(){
    }
}

by org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompile 通过org.abstractmeta.toolbox.compilation.compiler.JavaSourceCompile

and I want to know is there any way to get the compiled bytecode (.class file) from classloader or any any Object 我想知道有什么方法可以从类加载器或任何对象中获取已编译的字节码(.class文件)

you need to write a custom class loader that overloads the findClass method 您需要编写一个自定义的类加载器,该类加载器将findClass方法重载

 public Class findClass(String name) {
     byte[] b = ... // get the bytes from wherever they are generated
     return defineClass(name, b, 0, b.length);
 }

When you're using the JavaSourceCompiler , you have to provide a CompilationUnit to both versions of the compile method. 使用JavaSourceCompiler ,必须为两个版本的compile方法提供一个CompilationUnit Call getOutputClassDirectory() on this CompilationUnit to get the directory that holds all of the compiled .class files. 在此CompilationUnit上调用getOutputClassDirectory()以获取包含所有已编译.class文件的目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM