You can use eclipse jdt compiler (http://www.eclipse.org/jdt/core). It is used in many projects to compile java.
Here is an example how tomcat uses it to compile servlets to .class: http://www.docjar.com/html/api/org/apache/jasper/compiler/JDTCompiler.java.html