Install Javonet for .NET
Javonet is available as a Nuget package, which can be installed using Nuget package manager or any other method which supports Nuget packages. Detailed instructions can be found at Nuget Documentation
For applications using .NET Framework 4.7.2 and higher use Javonet.Clr.Sdk Package
For application using .NET Core 3.1 or .NET 5.0 and higher use Javonet.Netcore.Sdk Package
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.
using Javonet.Netcore.Sdk;
If Javonet.Clr.Sdk package is used replace it with "using Javonet.Clr.Sdk."
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.
var 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:
var pythonType = pythonRuntime.GetType("math").Execute();
And then get static field from the type:
var response = pythonType.GetStaticField("pi").Execute();
The returned value needs to be cast to calling technology type and can be used as any other variable:
var result = (float)response.GetValue();
System.Console.WriteLine(result);
To sum up, the whole code snippet looks like:
// <Activation>
Javonet.Activate("your-license-key");
// </Activation>
// <RuntimeContextCreation>
var pythonRuntime = Javonet.InMemory().Python();
// </RuntimeContextCreation>
// <GetType>
var pythonType = pythonRuntime.GetType("math").Execute();
// </GetType>
// <GetStaticField>
var response = pythonType.GetStaticField("pi").Execute();
// </GetStaticField>
// <GetValue>
var result = (float)response.GetValue();
System.Console.WriteLine(result);
// </GetValue>
Was this article helpful?