Getting started for Java

Javonet allows you to reference and use modules or packages written in (C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology.

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".

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).

Prerequisites

To call library/package/module from another technology, corresponding runtime has to be installed. See Prerequisites for details about installing called technology runtime.
Javonet is available as a JAR library which can be downloaded from public repository or from My Javonet Portal.

<dependency>
    <groupId>com.javonet</groupId>
    <artifactId>javonet-java-sdk</artifactId>
    <version>2.4.1</version> <!--Update with latest version-->
</dependency>

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.

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. 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.

RuntimeContext pythonRuntime = Javonet.inMemory().python();

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 = (double) response.getValue();
System.out.println(result);

To sum up, the whole code snippet looks like:

// <Import>
import com.javonet.sdk.internal.InvocationContext;
import com.javonet.sdk.internal.RuntimeContext;
import com.javonet.sdk.java.Javonet;
// </Import>

public class SampleProgram {

    public static void main(String[] args) {
		// <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 = (double) response.getValue();
        System.out.println(result);
		// </GetValue>
    }
}

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.