1

assume that I have this String : and i need to generate a class of it inside another class ? Kindly any one have a suggestion for this ?**

 String sourceCode = "public class Test {   " +
        "public void myMethod(@Mandatory String s){"+
        "System.out.println( \"myMethod : \"+s);}"+


 "@SuppressWarnings(\"unused\")"+
 "private void mySecondMethod(String s2){"+
    "System.out.println(\"mySecondMethod : \"+s2);}"+ 

 "}";   
1

2 Answers 2

1

You can generate java classes dynamically using Java Compiler API

http://www.accordess.com/wpblog/an-overview-of-java-compilation-api-jsr-199/

1

You can use the JavaCompiler.

There are examples in the link.

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.