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);}"+ 

 "}";   
Sergey K.
  • 24,894
  • 13
  • 106
  • 174
  • Look at this question. http://stackoverflow.com/questions/616532/on-the-fly-in-memory-java-code-compilation-for-java-5-and-java-6 – ejb_guy May 23 '12 at 07:33

2 Answers2

1

You can generate java classes dynamically using Java Compiler API

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

UVM
  • 9,776
  • 6
  • 41
  • 66
1

You can use the JavaCompiler.

There are examples in the link.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118