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...
Asked
Active
Viewed 572 times
-5
-
Search for Java Compiler API. – Rohit Jain Aug 07 '13 at 13:31
2 Answers
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");
}
}
}

Narendra Pathai
- 41,187
- 18
- 82
- 120