{"id":998340,"date":"2024-12-27T09:32:12","date_gmt":"2024-12-27T01:32:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998340.html"},"modified":"2024-12-27T09:32:24","modified_gmt":"2024-12-27T01:32:24","slug":"python%e5%92%8cjava%e5%a6%82%e4%bd%95%e8%bf%9e%e9%80%9a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998340.html","title":{"rendered":"python\u548cjava\u5982\u4f55\u8fde\u901a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073827\/53ff755f-7b37-4b02-8e3a-5a892479c4e1.webp\" alt=\"python\u548cjava\u5982\u4f55\u8fde\u901a\" \/><\/p>\n<p><p> <strong>Python\u548cJava\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u8fde\u901a\uff0c\u5305\u62ec\u4f7f\u7528Java Native Interface (JNI)\u3001Jython\u3001Py4J\u3001RESTful API\u3001\u6d88\u606f\u961f\u5217\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u9879\u76ee\u67b6\u6784\u3002<\/strong>\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c\u4f7f\u7528Py4J\u8fdb\u884c\u901a\u4fe1\u662f\u8f83\u4e3a\u7b80\u4fbf\u4e14\u9ad8\u6548\u7684\u65b9\u5f0f\u3002Py4J\u5141\u8bb8Python\u7a0b\u5e8f\u8c03\u7528Java\u865a\u62df\u673a\u4e2d\u7684Java\u4ee3\u7801\uff0c\u5e76\u5728\u4e24\u4e2a\u8bed\u8a00\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002<strong>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u9700\u8981\u7f16\u5199\u590d\u6742\u7684\u672c\u5730\u4ee3\u7801<\/strong>\uff0c\u5e76\u4e14\u652f\u6301\u5927\u591a\u6570\u57fa\u672c\u6570\u636e\u7c7b\u578b\u548c\u96c6\u5408\u7684\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Py4J\u8fdb\u884c\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>Py4J\u662f\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u5141\u8bb8Python\u5e94\u7528\u7a0b\u5e8f\u8c03\u7528Java\u4ee3\u7801\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684API\u6765\u5b9e\u73b0Python\u5230Java\u7684\u8c03\u7528\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u5728\u4e24\u4e2a\u8bed\u8a00\u4e4b\u95f4\u9891\u7e41\u4ea4\u6362\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\u4e86Py4J\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py4j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Java\u7aef\uff0c\u9700\u8981\u5c06Py4J\u7684JAR\u5305\u6dfb\u52a0\u5230\u4f60\u7684\u9879\u76ee\u4e2d\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u4e0b\u8f7d\u548c\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e0b\u8f7dPy4J\u7684JAR\u6587\u4ef6\uff1a\u53ef\u4ee5\u4ecePy4J\u7684\u5b98\u65b9\u7f51\u7ad9\u6216Maven\u4e2d\u592e\u4ed3\u5e93\u4e0b\u8f7d\u3002<\/li>\n<li>\u5c06JAR\u6587\u4ef6\u6dfb\u52a0\u5230\u4f60\u7684Java\u9879\u76ee\u4e2d\uff1a\u66f4\u65b0\u4f60\u7684Java\u9879\u76ee\u7684\u6784\u5efa\u8def\u5f84\uff0c\u4ee5\u5305\u542bPy4J\u7684JAR\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><h4>2. \u521b\u5efaJava\u7f51\u5173<\/h4>\n<\/p>\n<p><p>\u5728Java\u7aef\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7f51\u5173\u670d\u52a1\u5668\uff0c\u4f7f\u5f97Python\u53ef\u4ee5\u8c03\u7528Java\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Java\u7c7b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import py4j.GatewayServer;<\/p>\n<p>public class JavaApplication {<\/p>\n<p>    public int addNumbers(int a, int b) {<\/p>\n<p>        return a + b;<\/p>\n<p>    }<\/p>\n<p>    public static void m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(String[] args) {<\/p>\n<p>        JavaApplication app = new JavaApplication();<\/p>\n<p>        GatewayServer server = new GatewayServer(app);<\/p>\n<p>        server.start();<\/p>\n<p>        System.out.println(&quot;Gateway Server Started&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Python\u8c03\u7528Java\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728Python\u7aef\uff0c\u53ef\u4ee5\u901a\u8fc7Py4J\u7684API\u8fde\u63a5\u5230Java\u7f51\u5173\u5e76\u8c03\u7528Java\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from py4j.java_gateway import JavaGateway<\/p>\n<p>gateway = JavaGateway()  # connect to the Java server<\/p>\n<p>java_app = gateway.entry_point  # get the Java application entry point<\/p>\n<p>result = java_app.addNumbers(10, 20)<\/p>\n<p>print(f&quot;Result from Java: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cPython\u7a0b\u5e8f\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528Java\u4ee3\u7801\uff0c\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Jython<\/h3>\n<\/p>\n<p><p>Jython\u662fPython\u8bed\u8a00\u7684Java\u5b9e\u73b0\uff0c\u5141\u8bb8\u5728Java\u5e73\u53f0\u4e0a\u8fd0\u884cPython\u4ee3\u7801\u3002\u5b83\u9002\u7528\u4e8e\u90a3\u4e9b\u5e0c\u671b\u5728Java\u73af\u5883\u4e2d\u76f4\u63a5\u8fd0\u884cPython\u4ee3\u7801\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u914d\u7f6e<\/h4>\n<\/p>\n<p><p>Jython\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u5b9e\u73b0\uff0c\u4e0d\u9700\u8981\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff0c\u800c\u662f\u9700\u8981\u4e0b\u8f7dJython\u7684\u5b89\u88c5\u7a0b\u5e8f\u3002\u53ef\u4ee5\u4eceJython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528Jython\u96c6\u6210<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7Jython\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728Java\u4e2d\u8fd0\u884cPython\u811a\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import org.python.util.PythonInterpreter;<\/p>\n<p>public class JythonExample {<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        PythonInterpreter interpreter = new PythonInterpreter();<\/p>\n<p>        interpreter.exec(&quot;print(&#39;Hello from Python&#39;)&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528RESTful API<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5206\u5e03\u5f0f\u7cfb\u7edf\u6216\u9700\u8981\u8de8\u5e73\u53f0\u901a\u4fe1\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528RESTful API\u3002\u901a\u8fc7\u5728Java\u548cPython\u4e4b\u95f4\u5efa\u7acbHTTP RESTful\u670d\u52a1\uff0c\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u4ea4\u6362\u548c\u529f\u80fd\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u5728Java\u4e2d\u521b\u5efaRESTful\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Spring Boot\u6216JAX-RS\u6846\u67b6\u53ef\u4ee5\u5feb\u901f\u521b\u5efaRESTful\u670d\u52a1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Spring Boot\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684REST\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">@RestController<\/p>\n<p>public class MyController {<\/p>\n<p>    @GetMapping(&quot;\/add&quot;)<\/p>\n<p>    public int add(@RequestParam int a, @RequestParam int b) {<\/p>\n<p>        return a + b;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5728Python\u4e2d\u8c03\u7528RESTful\u670d\u52a1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684requests\u5e93\u53ef\u4ee5\u8f7b\u677e\u8c03\u7528Java\u63d0\u4f9b\u7684RESTful\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&quot;http:\/\/localhost:8080\/add&quot;, params={&quot;a&quot;: 10, &quot;b&quot;: 20})<\/p>\n<p>result = response.json()<\/p>\n<p>print(f&quot;Result from REST API: {result}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6d88\u606f\u961f\u5217<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u9700\u8981\u5f02\u6b65\u901a\u4fe1\u7684\u5e94\u7528\u573a\u666f\uff0c\u6d88\u606f\u961f\u5217\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u53ef\u4ee5\u4f7f\u7528RabbitMQ\u3001Apache Kafka\u7b49\u6d88\u606f\u961f\u5217\u5de5\u5177\u5728Python\u548cJava\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u6d88\u606f\u961f\u5217<\/h4>\n<\/p>\n<p><p>\u4ee5RabbitMQ\u4e3a\u4f8b\uff0c\u9996\u5148\u9700\u8981\u5728\u7cfb\u7edf\u4e2d\u5b89\u88c5\u548c\u914d\u7f6eRabbitMQ\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><h4>2. Java\u6d88\u606f\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005<\/h4>\n<\/p>\n<p><p>\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528RabbitMQ\u7684Java\u5ba2\u6237\u7aef\u5e93\u53d1\u5e03\u548c\u63a5\u6536\u6d88\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">ConnectionFactory factory = new ConnectionFactory();<\/p>\n<p>factory.setHost(&quot;localhost&quot;);<\/p>\n<p>try (Connection connection = factory.newConnection();<\/p>\n<p>     Channel channel = connection.createChannel()) {<\/p>\n<p>    channel.queueDeclare(&quot;task_queue&quot;, true, false, false, null);<\/p>\n<p>    String message = &quot;Hello from Java&quot;;<\/p>\n<p>    channel.basicPublish(&quot;&quot;, &quot;task_queue&quot;, null, message.getBytes());<\/p>\n<p>    System.out.println(&quot; [x] Sent &#39;&quot; + message + &quot;&#39;&quot;);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. Python\u6d88\u606f\u6d88\u8d39\u8005\u548c\u751f\u4ea7\u8005<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pika\u5e93\u4e0eRabbitMQ\u8fdb\u884c\u901a\u4fe1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>channel = connection.channel()<\/p>\n<p>channel.queue_declare(queue=&#39;task_queue&#39;, durable=True)<\/p>\n<p>def callback(ch, method, properties, body):<\/p>\n<p>    print(f&quot;Received {body}&quot;)<\/p>\n<p>channel.basic_consume(queue=&#39;task_queue&#39;, on_message_callback=callback, auto_ack=True)<\/p>\n<p>print(&#39;Waiting for messages. To exit press CTRL+C&#39;)<\/p>\n<p>channel.start_consuming()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u548cJava\u4e4b\u95f4\u5b9e\u73b0\u901a\u4fe1\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u5408\u9002\u7684\u6280\u672f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<strong>Py4J\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u4e14\u76f4\u63a5\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e24\u8005\u4e4b\u95f4\u7684\u8c03\u7528\uff0c\u800cJython\u5219\u5141\u8bb8\u5728Java\u5e73\u53f0\u4e0a\u76f4\u63a5\u8fd0\u884cPython\u4ee3\u7801\u3002<\/strong>\u5bf9\u4e8e\u9700\u8981\u8de8\u5e73\u53f0\u901a\u4fe1\u6216\u5206\u5e03\u5f0f\u7cfb\u7edf\uff0cRESTful API\u548c\u6d88\u606f\u961f\u5217\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u5f00\u53d1\u8005\u5e94\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u548cJava\u4e4b\u95f4\u5171\u4eab\u6570\u636e\uff1f<\/strong><br \/>Python\u548cJava\u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5171\u4eab\u6570\u636e\uff0c\u5305\u62ec\u4f7f\u7528RESTful API\u3001\u6d88\u606f\u961f\u5217\u6216\u901a\u8fc7\u6570\u636e\u5e93\u3002\u6784\u5efa\u4e00\u4e2aRESTful API\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0cPython\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u6846\u67b6\uff0c\u800cJava\u53ef\u4ee5\u4f7f\u7528Spring Boot\u7b49\u6846\u67b6\u3002\u6d88\u606f\u961f\u5217\u5982RabbitMQ\u6216Kafka\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5f02\u6b65\u6570\u636e\u4f20\u8f93\u3002\u82e5\u9009\u62e9\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u5171\u540c\u8bbf\u95ee\u540c\u4e00\u6570\u636e\u5e93\u6765\u5b9e\u73b0\u6570\u636e\u5171\u4eab\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8c03\u7528Java\u4ee3\u7801\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5728Python\u4e2d\u8c03\u7528Java\u4ee3\u7801\u3002\u4f7f\u7528JPype\u5e93\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Java\u7c7b\u548c\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Jython\uff0c\u8fd9\u662f\u4e00\u4e2a\u5728Java\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684Python\u5b9e\u73b0\u3002\u6b64\u5916\uff0cPy4J\u5e93\u4e5f\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u9009\u62e9\uff0c\u5b83\u5141\u8bb8Python\u4ee3\u7801\u8c03\u7528Java\u5bf9\u8c61\u3002<\/p>\n<p><strong>Java\u5982\u4f55\u8c03\u7528Python\u811a\u672c\uff1f<\/strong><br \/>Java\u53ef\u4ee5\u901a\u8fc7Runtime.exec()\u65b9\u6cd5\u6216ProcessBuilder\u6765\u6267\u884cPython\u811a\u672c\u3002\u9700\u8981\u786e\u4fdd\u5728\u7cfb\u7edf\u4e2d\u5df2\u5b89\u88c5Python\uff0c\u5e76\u4e14\u811a\u672c\u7684\u8def\u5f84\u6b63\u786e\u3002\u6b64\u5916\uff0c\u4f7f\u7528Jython\u6216Py4J\u7b49\u5e93\u4e5f\u80fd\u5b9e\u73b0\u66f4\u7d27\u5bc6\u7684\u96c6\u6210\uff0c\u65b9\u4fbfJava\u4ee3\u7801\u4e0ePython\u811a\u672c\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u548cJava\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u8fde\u901a\uff0c\u5305\u62ec\u4f7f\u7528Java Native Interface (JNI) [&hellip;]","protected":false},"author":3,"featured_media":998352,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998340"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=998340"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998340\/revisions"}],"predecessor-version":[{"id":998354,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998340\/revisions\/998354"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998352"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}