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,096
  • 13
  • 100
  • 173
  • 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,725
  • 5
  • 40
  • 64
1

You can use the JavaCompiler.

There are examples in the link.

Jean Logeart
  • 50,693
  • 11
  • 81
  • 116