# Compile and Run Java Source Code in Memory

In this blog I will show you how to compile and run Java source code on the fly in memory. In other words, use Java as a script language.

Here are the three source code files.

Explanations:

1. In order to represent a Java source file in memory instead of disk, I defined a StringInputBuffer class in the MemoryJavaFileManager.java.
2. To save the compiled .class files in memory, I implemented a class MemoryJavaFileManager. The main idea is to override the function getJavaFileForOutput() to store bytecodes into a map.
3. To load the bytecodes in memory, I have to implement a customized classloader MemoryClassLoader, which reads bytecodes in the map and turn them into classes.

Here is a unite test.