Compile a Java code in Java
Description
The following code shows how to compile a Java code.
Example
//from w w w .ja v a 2s . c o m
import java.io.IOException;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Main {
public static void main(String args[]) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int results = compiler.run(null, null, null, "Foo.java");
System.out.println("Success: " + (results == 0));
}
}
// File: MyClass.java
class MyClass {
public static void main(String args[]) {
System.out.println("Hello, World");
}
}
The code above generates the following result.