依赖: net.sf.py4jpy4j0.10.8.1
public class ExampleClientApplication { public static void main(String[] args) { getClient(); } public static void getClient(){ try{ ClientServer clientServer = new ClientServer(null); // We get an entry point from the Python side IHello hello = (IHello) clientServer.getPythonServerEntryPoint(new Class[] { IHello.class }); // Java calls Python without ever having been called from Python //System.out.println(hello.sayHello()); //创建数组,并给数组赋值 char[][] arr = new char[2][65535]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < 65535; j++) { arr[i][j] = Character.valueOf((char) j); } } //打印:调用python方法后返回的结果 System.out.println(hello.sayHello(arr, "hpg")); clientServer.shutdown(); }catch (Exception e){ System.out.println("--------------------------"); e.printStackTrace(); } }
}
包含有python函数的接口类:
package py4j.examples;
import java.math.BigDecimal;
public interface IHello {
public String sayHello(); public String sayHello(char[][] i, String s);
}