Install Javonet for Java
Javonet is available as a JAR library and can be used as any other JAR package. Javonet 2 is recommended for most applications.
Javonet 1.0 in some cases may be more appropriate (f.e. for embedding .NET UI controls (WPF or WinForms) in Java AWT/Swing or JavaFX).
Activate Javonet
Use register or log in pages to obtain license key. An e-mail and license key is necessary to activate Javonet.
First sample application
Javonet needs to be imported as any other dependency.
import com.javonet.sdk.internal.InvocationContext;
import com.javonet.sdk.internal.RuntimeContext;
import com.javonet.sdk.java.Javonet;
Javonet needs to be activated first. Activation must be called only once at the start-up of an application. During the first activation, license server are called and a javonet.lic file is generated.
Javonet.activate("your-license-key");
To use other programming technology, Runtime Context of the called technology needs to be created.
RuntimeContext pythonRuntime = Javonet.inMemory().python();
RuntimeContext refers to single instance of the called runtime. Once it is created it is used to interact with called runtime. The simplest use case is to get from target technology a type from a built-in library:
InvocationContext pythonType = pythonRuntime.getType("math").execute();
And then get static field from the type:
InvocationContext response = pythonType.getStaticField("pi").execute();
The returned value needs to be cast to calling technology type and can be used as any other variable:
float result = (float) response.getValue();
System.out.println(result);
To sum up, the whole code snippet looks like:
// <Activation>
Javonet.activate("your-license-key");
// </Activation>
// <RuntimeContextCreation>
RuntimeContext pythonRuntime = Javonet.inMemory().python();
// </RuntimeContextCreation>
// <GetType>
InvocationContext pythonType = pythonRuntime.getType("math").execute();
// </GetType>
// <GetStaticField>
InvocationContext response = pythonType.getStaticField("pi").execute();
// </GetStaticField>
// <GetValue>
float result = (float) response.getValue();
System.out.println(result);
// </GetValue>
Was this article helpful?