title: On-the-fly, in-memory java code compilation for Java 5 and Java 6
body: <p>How can I compile java code from an arbitrary string (in memory) in Java 5 and Java 6, load it and run a specific method on it (predefined)?</p>
  
  <p>Before you flame this, I looked over existing implementations:</p>
  
  <ul>
  <li>Most rely on Java 6 Compiler API.</li>
  <li>Those that don't, rely on tricks.</li>
  <li>Yes, I checked out commons-jci. Either I'm too dense to understand how it works, or it just doesn't.</li>
  <li>I could not find how to feed the compiler my current class path (which is quite huge).</li>
  <li>On the implementation that worked (in Java 6), I could not find how to correctly load inner classes (or inner anonymous classes).</li>
  <li>I'd quite like it if the entire thing was in-memory, as the thing runs on multiple environments.</li>
  </ul>
  
  <p>I'm sure this has been solved before, but I can't find anything that looks even half-production quality on google (except jci, which, as I've said before, I haven't managed to use).</p>
  
  <p>Edit: </p>
  
  <ul>
  <li>I looked over JavaAssist - I need inner classes, Java 5.0 language level support and compiling with the entire classpath. Also, I'd like to create new classes on the fly. I 
  might be mistaken, but I couldn't find how to do this with JavaAssit.</li>
  <li>I'm willing to use a file-system based solution (calling javac) but I don't know how to divine the classpath, nor how to later load the files (which are not in my classpath) with a special classloader that can be recycled for multiple invocations. While I do know how to research it, I'd prefer a ready solution.</li>
  </ul>
  
  <p>Edit2:
  For now, I'm content with BeanShell "evaluate". Apparently it does everything I need it to (get a string, evaluate it in the context of the 'current' classpath. It does miss some of Java 5 features, but it can use enums (not define) and compiled 'generic' (erased) classes, so it should be enough for what I want.</p>
  
  <p><del>I don't want to mark the answer as accepted yet since I do hope for a better solution to come up.</del></p>
  
  <p>Edit3: Accepted the beanshell suggestion - it really works wonderfully.</p>