Saturday, January 9, 2010

Dynamic Java Compilation to an Output Directory

Working on my top-secret free Bomes killer (to be released soon, it's already working), I ended up asking how to compile Java dynamically to an output directory of my choosing. It's been a lot of going back and forth with Carl Smotricz in this post on StackOverflow. Anyway, I've modified his code just a bit and here's what you get. Hope this helps somebody, someday. At least it helps me.

Excuse my cheesy Array manipulation. I'm out of touch with the right way to do it in Java, though converting to and from an ArrayList would not make my code shorter.

Mad props to Carl, of course!

4 comments:

Unknown said...

I know this is a rather old post, but I think this is a simple solution to a big problem I'm having. Is there a way to run resulting class after it is compiled from within the DynamicCompiler class?

Dan Rosenstark said...

Yes Adam, there is. Check out my own use here

https://github.com/drosenstark/mjdj/blob/master/src/com/confusionists/mjdj/fileIO/DeviceLoaderJava.java

Unknown said...

This works great with simple programs, however, I can't seem to get it to work at all when I need to import external libraries. I have a project working well in netbeans that I would like to dynamically compile, but since it imports libraries, I can't figure it out. Any ideas?

Dan Rosenstark said...

Hi Adam. I have not tried anything with this. Have you checked out http://stackoverflow.com/questions/60764 ?