Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

isdom/Java-Runtime-Compiler

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
41 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java-Runtime-Compiler

This takes a String, compiles it and loads it returning you a class from what you built.
By default it uses the current ClassLoader. It supports nested classes, but otherwise builds one class at a time.

On maven central

You can include in your project with

<dependency>
    <groupId>net.openhft</groupId>
    <artifactId>compiler</artifactId>
    <version>2.1.2</version>
</dependency>

Simple example

You needa CachedCompiler and access to you JDK's tools.jar.

 private final CachedCompiler cc = new CachedCompiler(null, null);
 
 // dynamically you can call
 Class aClass = cc.loadFromJava(className, javaCode);
 KnownInterface o = (KnownInterface) aClass.nextInstance();

I suggest making you class implement a "KnownInterface" as this will allow you to call/manipulate instances of you generated class.

Another more hacky way is to use this to override a class, provided it hasn't been loaded already.
This means you can redefine an existing class and provided the methods and fields used match, you have compiler redefine a class and code already compiled to use the class will still work.

About

Java Runtime Compiler

Resources

License

Stars

Watchers

Forks

Packages

No packages published
Morty Proxy This is a proxified and sanitized view of the page, visit original site.