WeBlog - Dynamic Java Code Execution

Card Puncher Data Processing

About

Running java code stored in a string variable created dynamically inside a java application (ie at runtime) is not straightforward in Java.

This article:

Sections

Why would you need it ?

There is for sure several cases and in this section, I highlight one.

During documentation creation, you show generally a code sample and its result.

Example: the below Java code

System.out.println("Hello Nico");

should output:

Hello Nico

As you can see, the documentation define a unit test.

The code being what should be performed and the result what is expected.

To implement this kind of unit test framework, you need to extract the code and to execute it before comparing it to the expected result. That's one business case for Java dynamic execution.

Let's see now why it's not straightforward to implement it in Java.

Why running generated Java code at runtime in Java is not straightforward ?

Java is a static language which means that a variable will never change of data type during the code execution.

This kind of language have one big advantage which is that they have a structure which means that every piece of code has:

This is then possible:

  • to parse it (and create a tree representation)
  • to see the flow of data
  • to detect fault before compile time
  • to refactor it

In other words, you need generally to compile the source code (written as text) with a compiler that produces machine language instruction. This instructions are the execution code.

They have still one drawback which is that it's really difficult to dynamically create and execute source code at runtime. For instance, Javascript is a dynamic language. It still compile the code but the code executed is a text file, this is not machine instruction.

The function that permits to do that is called generally the eval function.

Example in javascript: eval function within the Browser - Javascript - (Web) Console

x=eval("1+1"); // x will got the value 2 assigned
console.log(x);

Java implements also a dynamic language framework with the script engine. This engine permits you to implement a dynamic language. Jython for instance implements the Python language with Java. Java comes also by default with a complete Javascript engine called Nashorn. That let's you execute Javascript code.

For instance, if you want to execute the above code 1+1 with Nashorn , you will write the below java code where you can find again the eval function.

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("1+1");

This code is still Javascript code and is not Java code. The next section will show you how to execute Java code in Java.

Executing dynamic java code inside Java

To execute Java code, you need to:

  • create dynamically your java source code
  • compile it to a class,
  • to load the class,
  • to use the reflexion Api to discover your method and to execute it.

All this steps are explained in this article: How you can execute a string representing java code inside Java: Java - Dynamic Java code execution (at runtime)





Discover More
Card Puncher Data Processing
Weblog - 2018

In 2018, the following WeBlogs were written : How to create Java code within a Java application and executing it. How to query OBIEE through a database client application.



Share this page:
Follow us:
Task Runner