Create Java instance and call instance method from Python package
This article provides an introduction to cross-technology creation of instances of classes and invocation of instance methods.
With Javonet you can interact with classes from Python package like they were available in Java but invocation must be performed through Javonet SDK API, passing type and method name as string.
Javonet allows you to pass any Java value type as argument to instance method from Python package. In example: int, float, string, char, long and other. For reference type arguments (instances of other classes) you can create such instance with Javonet and pass the Invocation Context variable referencing that object as argument of static method invocation.
Using Java in custom Python package
With Javonet it is possible to reference any custom Python package and interact with public methods declared on types defined within that module almost the same as with any other Java library.
This section present sample custom Python package with class declaring methods and Javonet SDK syntax required to create object and invoke that methods and consume the results in Java.
Snippet below represents the sample code from Python package which contains class and its methods:
@staticmethod
def multiply_by_two(a):
return 2 * a
def multiply_two_numbers(self, a, b):
return a * b
It is possible to invoke one of the declared instance methods from Python package using following Java code.
// use activate only once in your app
Javonet.activate("your-license-key");
// create called runtime context
RuntimeContext calledRuntime = Javonet.inMemory().python();
// set up variables
// path to directory with .py files
String libraryPath = resourcesDirectory;
String className = "TestClass.TestClass";
// load custom library
calledRuntime.loadLibrary(libraryPath);
// create type's instance
InvocationContext instance = Javonet.inMemory().python().getType(className).createInstance(0, 1).execute();
// invoke instance's method
InvocationContext response = instance.invokeInstanceMethod("multiply_two_numbers", 5, 4).execute();
// get value from response
int result = (int) response.getValue();
// write result to console
System.out.println(result);
In code snippet above you can see how easily you can activate Javonet and instruct it using inMemory() method to create new RuntimeContext that will run python-package runtime within your current process. Next with addLibrary method it triggers the load of required python-package module and allows you to interact with any classes and their methods defined in that package.
Further call to createInstance() allows to create instance of class. Calls to invokeInstanceMethod() allows to call "multiplyTwoNumbers" python-package instance method and pass the value type arguments. With Javonet you can invoke methods with any number and any type of arguments including value type arguments, reference type arguments, arrays and collections.
You can receive and further process and type of result returned by called python-package method, regardless if it is reference type that will get returned as another instance of Invocation Context that you can use for further interaction, or value type that you can obtain as java value with getValue() method.
Using Java framework Python package
The same steps are required to use types and methods from standard Python package:
The same operation can be performed remotely by just changing the new Runtime Context invocation from in memory to tcp that will create and interact with your Python package objects on any remote node, container or service that hosts Javonet Code Gateway. This way you can preserve the same logic in your application and instantly switch between monolithic and microservices architecture without the need to implement the integration layer based on web services or other remote invocation methods.
Read more about use cases and software architecture scenarios where Javonet runtime bridging technology can support your development process.
Was this article helpful?