在Ubuntu上實現Python與Java的互操作可以通過多種方式實現,以下是幾種常見的方法:
JPype是一個允許Python代碼直接調用Java類的庫。以下是基本步驟:
安裝JPype:
pip3 install jpype1
打包Java代碼為JAR: 編譯Java類并打包為JAR文件。例如:
javac testDemo.java
jar cvf testDemo.jar testDemo.class
在Python中調用Java:
from jpype import *
import os
# 啟動Java虛擬機
startJVM("/usr/lib/jvm/java-11-openjdk-amd64/bin/java", "-ea", "-Djava.class.path=/path/to/testDemo.jar")
# 加載Java類
JClass("testDemo")
# 調用Java方法
result = JClass("testDemo").inputTest("Hello")
print(result)
# 關閉Java虛擬機
shutdownJVM()
Jython是Python語言的Java實現,允許Python代碼直接調用Java類。以下是基本步驟:
安裝Jython:
wget https://downloads.apache.org//jython/2.7.2/jython-standalone-2.7.2.jar
java -jar jython-standalone-2.7.2.jar
編寫Java代碼(例如testDemo.java
):
package com.example;
public class testDemo {
public String inputTest(String input) {
return "Input content: " + input;
}
}
在Jython中調用Java:
from com.example import testDemo
demo = testDemo()
print(demo.inputTest("Hello"))
Apache Thrift是一個跨語言的服務定義框架,支持多種語言包括Python和Java。以下是基本步驟:
定義Thrift文件(例如example.thrift
):
namespace java com.example
namespace py com.example
service SharedService {
string constMap(1: string mapConstant)
struct Work {
1: i32 num1
2: i32 num2
3: Operation op
4: optional string comment
}
enum Operation {
ADD = 1
SUBTRACT = 2
MULTIPLY = 3
DIVIDE = 4
}
struct SharedStruct {
1: i32 key
2: string value
}
sharedStruct getSharedStruct(1: string key)
}
生成Java和Python代碼:
thrift --gen java example.thrift
thrift --gen py example.thrift
在Java中實現服務:
package com.example;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.impl.TMultiplexedProtocol;
import org.apache.thrift.impl.TProtocolFactory;
import org.apache.thrift.impl.TTransportFactory;
public class SharedServiceImpl implements SharedService.Iface {
@Override
public Map<String, String> constMap(String mapConstant) {
Map<String, String> result = new HashMap<>();
result.put("mapConstant", mapConstant);
return result;
}
public static void main(String[] args) {
try {
TTransport transport = new TSocket("localhost", 9090);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
SharedService.Client client = new SharedService.Client(protocol);
TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, "shared");
SharedService.Client sharedClient = new SharedService.Client(multiplexedProtocol);
Map<String, String> result = sharedClient.constMap("hello:world");
System.out.println(result);
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
在Python中調用Java服務:
from shared_service import SharedService
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
transport = TSocket.TSocket('localhost', 9090)
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = SharedService.Client(protocol)
result = client.constMap({'hello': 'world'})
print(result)
transport.close()
通過以上方法,您可以在Ubuntu上實現Python與Java的互操作,選擇適合您項目需求的方法進行實現。