0

How can i execute java code from my java program? Is there a framework I could use to programatically execute them? please help, thanks.

My java class

public class test {

    public static compute(int one, int two){
         // compute goes here
   }

    public static void main(String[] args) {
        //read from file
        //execute the commands
    }
}

my text file : contains java code fragments doesn't consists the whole java file.

   System.out.println("Start of my program");

@do("compute my two int"){
  val = compute(15,25);
  System.out.println(val);
}
Njax3SmmM2x2a0Zf7Hpd
  • 1,354
  • 4
  • 22
  • 44

1 Answers1

0

The JDK contains the JavaCompiler API. You can use it to compile text to a .class file at runtime, and then proceed to make this file available on the classpath—and finally load it using reflection.

The above, if repeated, will result in a "class leak": all the classes you have ever loaded will be stuck in the runtime. To avoid that you'll need to create a new classloader for each class you load dynamically.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436