I am making software that will need to dynamically create java bytecode, and possibly even make it a runnable jar.
My current idea is to create a new .java file and compile it at runtime. I can create the file, but I'm not sure how to compile it at runtime, and make it a runnable jar. Any help would be greatly appreciated.
public static String generate(String filePath) {
try {
File file = new File("Test.java");
if(!file.exists())file.createNewFile();
FileWriter write = new FileWriter(filePath+".java");
BufferedWriter out = new BufferedWriter(write);
out.write(
"public class Test {\n\t" +
"public static void main(String[] args){\n\t\t"+
"System.out.println(\"Working\");\n\t"+
"}\n" +
"}"
);
out.close();
} catch (Exception e) {// Catch exception if any
return "Error: " + e.getMessage();
}
return "Test.java created!";
}