Search code examples
javacompilationjiton-the-fly

Compile a Java program on the server right before a User downloads the program


I did a quick search and could not find anything on this topic. I am not even sure if this is possible, but I am curious.

Is it possible to compile a Java program on a server right before a user downloads the program. The application that I can think of for this would be to modify a program's source code on the fly before a user downloads it. This could be helpful in a setup where the program is modified based on user input or settings on a website and those changes are hard baked into the program so what they download is a stand alone program that is customized and fully portable. The other application I thought of would be if each user were to use a different feature combination in a program so it is compiled only with the feature set they need/want on the fly.

I have a few programs ideas that I could test this out with, but this is mostly an academic thought and curiosity of mine.

So long story short, does anyone know of any technologies that could make a system like this work?


Solution

  • I have written a library using the Compile API (comes with Java 6) to compile code in ememory but I would suggest you don't need to pre-generate code as anything you can do with generated code you can do with dynamic code. There can be a slight performance advantage, but I suggest you try doing what you need with dynamic code (i.e. code with loops, if statements and reflection) to do what your generated code would do first as this is alot simpler and likely to do what you want.

    Even if you must have generated code, it is useful to write the code in a non-generated form first so you are clear as to what you need the code to do.