Tasawer Khan
Tasawer Khan

Reputation: 6148

Generating, compiling and using Java code at run time?

I have a scenario where I have to generate Java code when my application is running.

Can compile and run than code from my application( as a separate application).

Is it possible to compile and then use it from the same application. Cant think of any possibility

Upvotes: 9

Views: 10751

Answers (3)

karmakaze
karmakaze

Reputation: 36134

Check out Create dynamic applications with javax.tools. Second time I've referenced this today--I swear I don't work for them.

Upvotes: 11

iruediger
iruediger

Reputation: 953

You can use an instance of JavaCompiler:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

Follow the link for the an example on how to use it.

Upvotes: 5

ratchet freak
ratchet freak

Reputation: 48186

using the ProcessBuilder or Runtime.exec() you can run any commandline app from your java code

this includes the javac compiler and java as a separate process

Upvotes: 1

Related Questions