1

Say I have a text file with a simple method:

public void sayHello() {
    System.out.println("HELLO!");
}

Is there anyway to take that code and programatically insert the code into an existing .java file? Or even create a new Java source file with the code provided in the text file? I have heard of solutions where you can generate source code using CodeModel, but I would have to write my own parser to handle all the possible code that would be read from the text file.

tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

0

Take a look here: How do I programmatically compile and instantiate a Java class?

You can use the javax.tools to compile the code and then load it into your JVM.

Oracle has a write-up as does IBM.

Paul Rubel
  • 26,632
  • 7
  • 60
  • 80