On-the-fly, in-memory java code compilation for Java 5 and Java 6
If you're not completely tied to compiling, solutions like Beanshell, groovy and the other scripting languages are easily embedded (in-fact, java has built-in support for plugging in a scripting language so your code doesn't even know what language the script is written in)
Beanshell should run any 100% java code IIRC, and I believe Groovy can run most java code--possibly all.
java in-memory on-the-fly class compilation (and loading)
I ended up using Bean Shell. It's not perfect, but it solved 99% of the issue.
Compiling java Code Available In A String within another java code
You want to have a look at javax.tools.JavaCompiler
and related classes.
The documentation contains examples of how to use them.
Note that the java compiler will only work if you have a JDK installed. A JRE is not enough.
Include Javac in Java applet
Java compiler is always with you. You should use java.lang.Compiler
class. But I believe you will have issues with SecurityManager.
If I were you I'd prefer to compile code on server side. It means that I'd develop servlet that is able to get the java source and compile it. The resulted java class should be available over HTTP under path that is configured to be accessible by applet, so your applet will be able to run this class after compilation.
Compile code fully in memory with javax.tools.JavaCompiler
I've run the above code in Mac OS Java 7. None of them works. So i wrote one
https://github.com/trung/InMemoryJavaCompiler
StringBuilder source = new StringBuilder()
.append("package org.mdkt;\n")
.append("public class HelloClass {\n")
.append(" public String hello() { return \"hello\"; }")
.append("}");
Class<?> helloClass = InMemoryJavaCompiler.compile("org.mdkt.HelloClass", source.toString());
Related Topics
How to Split a Text into Sentences Using the Stanford Parser
Take Full Page Screenshot in Chrome with Selenium
How to Delete a Folder with Files Using Java
How to Remove Element from Arraylist by Checking Its Value
How to Copy Data from File to Postgresql Using Jdbc
How to Upload Files on Server Folder Using Jsp
Javamail with Gmail: 535-5.7.1 Username and Password Not Accepted
Refreshing Static Content with Spring MVC and Boot
Any Good Recommendations for Mp3/Sound Libraries for Java
Java Conventions: Use Getters/Setters Within the Class
How to Mock New Date() in Java Using Mockito
Jersey 2.X Custom Injection Annotation with Attributes
How to Simulate a Real Mouse Click Using Java
Extract Source Code from .Jar File
Constructor Synchronization in Java
Concurrenthashmap VS Synchronized Hashmap
Java: Subtract '0' from Char to Get an Int... Why Does This Work