Getting started for C++
Javonet is an advanced library enabling direct method calls between programming languages and modules. To use the guides both interacting technologies needs to be selected from left-side dropdown lists. Developer's technology is named "I code in" and technology to be called is named "I want to use".
Prerequisites
To call library/package/module from another technology, corresponding runtime has to be installed. See About Javonet for details about installing called technology runtime.
Javonet is available as a dynamic c++ library and can be downloaded from My Javonet Portal.
Get activation key
Use register or log in page to get license key, which is necessary to activate Javonet.
First sample application
Javonet needs to be imported as any other dependency.
#include "Javonet.h"
Javonet needs to be activated first. Activation must be called only once at the start-up of an application. More about activation in Activating Javonet section
Javonet::Activate("your-license-key");
As a second step, Runtime Context of the called technology needs to be created. RuntimeContext refers to single instance of the called runtime. Once it is created it is used to interact with called runtime.
auto 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:
auto pythonType = pythonRuntime->GetType("math")->Execute();
And then get static field from the type:
auto response = pythonType->GetStaticField("pi")->Execute();
The returned value needs to be cast to calling technology type and can be used as any other variable:
auto result = std::any_cast<float>(response->GetValue());
std::cout << result << std::endl;
To sum up, the whole code snippet looks like:
// <Import>
#include "Javonet.h"
// </Import>
#include <iostream>
using namespace JavonetNS::Cpp::Sdk;
std::string resourcesDirectory = ".";
int main(int argc, char* argv[]) {
// <Activation>
Javonet::Activate("your-license-key");
// </Activation>
// <RuntimeContextCreation>
auto pythonRuntime = Javonet::InMemory()->Python();
// </RuntimeContextCreation>
// <GetType>
auto pythonType = pythonRuntime->GetType("math")->Execute();
// </GetType>
// <GetStaticField>
auto response = pythonType->GetStaticField("pi")->Execute();
// </GetStaticField>
// <GetValue>
auto result = std::any_cast<float>(response->GetValue());
std::cout << result << std::endl;
// </GetValue>
}
Was this article helpful?