-5

I have several .java files created during runtime. I want to use those created .java files as classes in my program. How may I do this, without starting the program a second time ? So I want to compile the .java files during runtime and use the new .class files from now on...

kke
  • 353
  • 2
  • 8

2 Answers2

1

Use the Java Compiler API:

public class SimpleCompileTest {
    public static void main(String[] args) {
        String fileToCompile = "test" + java.io.File.separator +"MyClass.java";
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        int compilationResult = compiler.run(null, null, null, fileToCompile);
            if(compilationResult == 0){
               System.out.println("Compilation is successful");
           }else{
               System.out.println("Compilation Failed");
           }
    }
}

Detailed Example with explanation

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
0

You can use the Compiler API included in the JDK. See here.

Kayaman
  • 72,141
  • 5
  • 83
  • 121