Skip to content

Commit

Permalink
Doing work
Browse files Browse the repository at this point in the history
  • Loading branch information
gregwhitaker committed Jan 13, 2018
1 parent 7c1d0b0 commit bfbf6ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# openhft-javaruntimecompiler-example

An example of using [OpenHFT Java-Runtime-Compiler](https://github.com/OpenHFT/Java-Runtime-Compiler) to compile java classes at runtime and execute them.

## Running the Example
1. Run the application, compile `Example1.java`, and execute the newly compiled class using the following command:

$ ./gradlew -Parg1=Example1 run

You should see the following:

> Task :run
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
This is Example1!

2. Run the application again, but this time use `Example2`:

$ ./gradlew -Parg1=Example2 run
You should see the following:

> Task :run
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
This is Example2!

## Bugs and Feedback
For bugs, questions, and discussions please use the [Github Issues](https://github.com/gregwhitaker/openhft-javaruntimecompiler-example/issues).

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/github/gregwhitaker/openhft/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
import org.apache.commons.io.IOUtils;

/**
*
* Starts the openhft-javaruntimecompiler-example application.
*/
public class Main {

/**
* Main entry-point of the application.
*
* @param args which class to compile and run (Example1, Example2)
* @throws Exception
*/
public static void main(String... args) throws Exception {
if (args.length <= 0) {
throw new IllegalArgumentException("Missing argument: Please specify which class to run (Example1, Example2).");
Expand All @@ -24,11 +30,13 @@ public static void main(String... args) throws Exception {
className = "openhft.example.Example2";
javaCode = IOUtils.toString(Main.class.getClassLoader().getResourceAsStream("Example2.java"));
} else {

throw new IllegalArgumentException("Invalid argument: Please specify which class to run (Example1, Example2).");
}

// Compile the class
Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);

// Run the class
Runnable runner = (Runnable) aClass.newInstance();
runner.run();
}
Expand Down

0 comments on commit bfbf6ca

Please sign in to comment.