0

I compiled a Class from text

public class Foo{
    public Foo(){
    }
}

by 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

2

2 Answers 2

0

you need to write a custom class loader that overloads the findClass method

 public Class findClass(String name) {
     byte[] b = ... // get the bytes from wherever they are generated
     return defineClass(name, b, 0, b.length);
 }
2
  • thanks mohsin exactly i nedd ...// since my class is not .class file i have a class stored in String and i compile that -runtime- and now i nedd the bytecode is generated by the compilation.toolbox that give me a java.lang.classLoader to load a class
    – Mehdi.M
    Aug 19, 2013 at 9:47
  • String clazz = "public class foo{pubic Foo(){}}"
    – Mehdi.M
    Aug 19, 2013 at 9:52
0

When you're using the JavaSourceCompiler, you have to provide a CompilationUnit to both versions of the compile method. Call getOutputClassDirectory() on this CompilationUnit to get the directory that holds all of the compiled .class files.

4
  • i try but this is an address in the classpath and there is no .class file
    – Mehdi.M
    Aug 19, 2013 at 10:25
  • File m = compilationUnit.getOutputClassDirectory(); if(m.exists()) System.out.println("ok"); else System.out.println("ops"); the result is ops
    – Mehdi.M
    Aug 19, 2013 at 10:29
  • Did you try that before or after you ran compile? Aug 19, 2013 at 10:33
  • i try after this line of code ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
    – Mehdi.M
    Aug 19, 2013 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.