Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling java Code Available In A String within another java code [duplicate]

Possible Duplicate:
On-the-fly, in-memory java code compilation for Java 5 and Java 6
Compiling Java file with code from within a Java file

i have a hello world class available in the string of a program as given in the example below,

public class CompileJavaString {   
  public static void main(String arg[]) {
     String s="public class HelloWorld{ public static void main(String arg[]) ";
     s=s+" { System.out.println(\"Hello World\"); }  } ";
     // this is the complete code of Hello World class taken as an example   

     // code to compile the class Hello World available in string and 
     // generate the HelloWorld.class file required here
  }  

}

can someone help to compile the code in a memory string available in example given above

like image 268
adeel iqbal Avatar asked Jan 17 '26 17:01

adeel iqbal


1 Answers

You want to have a look at javax.tools.JavaCompiler and related classes. The documentation contains examples of how to use them.

Note that the java compiler will only work if you have a JDK installed. A JRE is not enough.

like image 112
Ingo Avatar answered Jan 20 '26 10:01

Ingo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!