compile Java source in memory (and run result)

Hello everybody,

I want to build Java sources dynamically and compile them - and of course run the generated classes FROM MEMORY (i.e. without generating files, just to stress this as much as I can :-)).

I know it's "always" been possible to invoke javac, but for one thing, I don't like accessing a ""-package, and also, this required java source files.

It looks like this has become a lot easier in Java 6 and I've come across a "solution" at

Unfortunately, it doesn't work 100%: The compilation succeeds, but the (attempted) call results in a ClassNotFoundException.

Who can help me solve this? I'd hope it's just a small thing... the folks at wouldn't post sample code that is untested and broken, would they? :-)
Who is Participating?

Improve company productivity with a Business Account.Sign Up

objectsConnect With a Mentor Commented:
Theres details on a classloader that should enable you to load the class in the following article

Its using the eclipse compiler but the concept is the same. Use of the eclipse compiler may also be of interest.

What problems are you having with the code? Please post the stack trace. It runs fine for me
Ronald112197Author Commented:
it prints:
Success: true
Class not found: java.lang.ClassNotFoundException: HelloWorld

java.lang.ClassNotFoundException: HelloWorld
      at Method)
      at java.lang.ClassLoader.loadClass(
      at sun.misc.Launcher$AppClassLoader.loadClass(
      at java.lang.ClassLoader.loadClass(
      at java.lang.ClassLoader.loadClassInternal(
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(
      at CompileSourceInMemory.main(

I'm running this in Eclipse - code style is 6.0, default VM is JDK 6.

Hmm... interesting - it actually works when I invoke it from the command line (I don't recompile, I just run it from the command line using JDK 6)... what could be causing this effect? Do I have to move the question to the "Eclipse"-section now? :-)
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

It's probably because Eclipse has its own ideas about the classpath
CEHJConnect With a Mentor Commented:
You can tell what the command line one is by doing the following, and what Eclipse's is by looking at the .classpath file
 out.println("public class HelloWorld {");
    out.println("  public static void main(String args[]) {");
    out.println("    System.out.println(\"This is in another java file and here is the classpath\");");
    out.println("    System.out.println(System.getProperty(\"java.class.path\"));");
    out.println("  }");

Open in new window

>  Do I have to move the question to the "Eclipse"-section now? :-)

probably a good idea. This is an eclipse problem, to do with its class loaders. In particular it only picks up classes in a source folder (which you're generated ones are not in)

> the folks at wouldn't post sample code that is untested and broken, would they? :-)

doubt much of their code is tested :)   though they aren't as bad as some of the sites around.

You would need to use a custom class loader to handle loading the class from memory

>>You would need to use a custom class loader to handle loading the class from memory

That's not the case (as the code you've just run shows)
Its a side effect of eclipse's classloader/classpath handling, the code runs fine when invoked from the command line
thats right, so using a custom class loader should get around it.
Gibu GeorgeConnect With a Mentor Chief Technology OfficerCommented:
The problem is that the class file is generated into the projects root folder if you change that to the default output folder it will work from eclipse. I change this line and it worked for me

JavaFileObject file = new JavaSourceFromString("HelloWorld", writer.toString());

JavaFileObject file = new JavaSourceFromString("../bin/HelloWorld", writer.toString());
where ../bin/ is the default output folder
Ronald112197Author Commented:
O I C.... :-)

See, this is me playing with code I copied and pasted without examining closely what it does. Naive as I am, I thought that a class called compileINmemory would actually compile in memory ;D

Looking at it again, I see that the page title is more accurate: "compile FROM memory" (to disk)

I don't like having to change the code to run from Eclipse, but the last comment pointed me to the simple solution: configure the program's working directory to be the "classes"-folder (or any other folder that's on the classpath). This is fine since I don't have to change the code for Eclipse, just configure Eclipse...

I guess my problem is solved and I'm ready to split the points between CEHJ and gibu_george.

However, I now don't like the code so much any more :-( What happens if I put my classes into a jar-file? The generated classes end up in whichever (working) directory the user happens to be in? (btw: Is the current directory ALWAYS on the classpath? I think that was not the case in older versions of Java...)

I'll toss in an extra 250 points for code that puts my class into a package (which for some reason isn't as easy as I'd thought... ) and actually compiles IN memory without creating files on disk... (which should be fine as an addition since MY question is actually called "compile IN memory"...


Ronald112197Author Commented:
P.S.: I think I've seen the solution (compile IN memory) in some blog, but I can't find it any more :-(
> configure the program's working directory to be the "classes"-folder (or any other folder that's on the classpath).

mentioned that earlier:
objects> In particular it only picks up classes in a source folder (which you're generated ones are not in)

>  and actually compiles IN memory without creating files on disk...

the example I posted above compiles in memory

Ronald112197Author Commented:
Hello experts,

sorry for my long absence - I really didn't have time to do this lately - but I've always had you on my mind ;-))

I have finally implemented the full solution and it works quite nicely (I also need the "dynamically compiled class" to interact with my other classes and even that seems to be no problem so far - I run from Eclipse so far and hope there won't be additional problems with the "deployed" version which uses a special classloader... we'll see...)

I'll split the points as follows:
100 to CEHJ for "continued support" and some pointers in the right direction, although it didn't quite nail it...

200 to gibu_george for clearly showing and solving the problem I had - although I'd like to mention that I don't like his proposed solution: Adjusting the code to make up for environment problems is not such a good idea. As I mentioned above, I reconfigured Eclipse instead and the code will work both in Eclipse and in a JDK... Anyway, it showed me what was going on... (should've figured that out by myself, I guess...)

200 "new points" to objects for posting the correct code sample that I am now using (and which solves all the problems I had with the previous sample). It's quite compact, self-contained and seems to work quite nicely. I think the link posted by CEHJ is basically the same, but it's split across several classes in different packages and I'd need to download and adjust all of them to remove dependencies to even other utility classes in the packages...
@objects: You said "Use of the Eclipse compiler may also be of interest" - why? It's an additional download, an additional dependency and an additional source of incompatibilities. Using the JDK compiler in Eclipse works just fine (it seems to be a little slow on the startup, though...). Any advantage in using the Eclipse compiler?

Ronald112197Author Commented:
Hello experts!

As I'd feared, I have problems with our special classloader (some classes are encrypted).
I have posted a new question about this, you can find it at

Maybe you have some ideas? (and I'll be a lot faster with the grading this time as I'm currently working on this and feel like I'm "almost there"...)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.