In this recipe, we will start a JVM and send a NumPy array to it. We will print the received array using standard Java calls. Obviously, you will need to have Java installed.
The complete code for this recipe is in the hellojpype.py
file in this book's code bundle:
import jpype import numpy as np #1. Start the JVM jpype.startJVM(jpype.getDefaultJVMPath()) #2. Print hello world jpype.java.lang.System.out.println("hello world") #3. Send a NumPy array values = np.arange(7) java_array = jpype.JArray(jpype.JDouble, 1)(values.tolist()) for item in java_array: jpype.java.lang.System.out.println(item) #4. Shutdown the JVM jpype.shutdownJVM()
First, we need to start the JVM from JPype:
Start the JVM from JPype; JPype is conveniently able to find the default JVM path:
jpype.startJVM(jpype.getDefaultJVMPath())
Just because of tradition, let's print
"hello world"
:jpype.java.lang.System.out.println("hello world")
Create a NumPy array, convert it into a Python...