{"id":988780,"date":"2024-12-27T08:05:18","date_gmt":"2024-12-27T00:05:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/988780.html"},"modified":"2024-12-27T08:05:23","modified_gmt":"2024-12-27T00:05:23","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8java%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/988780.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528java\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064303\/cdb08f4a-3157-400a-acdd-0f4bf9a467d4.webp\" alt=\"python\u5982\u4f55\u8c03\u7528java\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528Java\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528JPype\u3001Py4J\u6216Jython\u7b49\u5de5\u5177\u5b9e\u73b0\uff0c\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u4f18\u52a3\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u3002<\/strong>\u5bf9\u4e8e\u7b80\u5355\u7684Java\u4ee3\u7801\u8c03\u7528\uff0c<strong>JPype<\/strong>\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8Python\u7a0b\u5e8f\u76f4\u63a5\u8c03\u7528Java\u4ee3\u7801\u800c\u65e0\u9700\u542f\u52a8JVM\u3002<strong>Py4J<\/strong>\u5219\u9002\u7528\u4e8e\u9700\u8981\u4e0eJava\u7a0b\u5e8f\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u7684\u573a\u666f\u3002<strong>Jython<\/strong>\u662f\u4e00\u79cd\u80fd\u591f\u8fd0\u884c\u5728JVM\u4e0a\u7684Python\u5b9e\u73b0\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Java\u4ee3\u7801\uff0c\u4f46\u5b83\u4e0d\u652f\u6301\u6240\u6709\u7684Python\u7279\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001JPYPE\u5de5\u5177<\/p>\n<\/p>\n<p><p>JPype\u662f\u4e00\u4e2a\u9879\u76ee\uff0c\u65e8\u5728\u901a\u8fc7Java\u672c\u5730\u63a5\u53e3\uff08JNI\uff09\u5b9e\u73b0Python\u4e0eJava\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u5b83\u5141\u8bb8Python\u4ee3\u7801\u76f4\u63a5\u8c03\u7528Java\u4ee3\u7801\uff0c\u5e76\u4e14\u65e0\u9700\u542f\u52a8Java\u865a\u62df\u673a\uff08JVM\uff09\u3002<\/p>\n<\/p>\n<ol>\n<li>JPype\u5b89\u88c5\u4e0e\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u8981\u4f7f\u7528JPype\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install JPype1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8c03\u7528Java\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Java\u7c7b\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u540d\u4e3a<code>HelloWorld<\/code>\u7684\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class HelloWorld {<\/p>\n<p>    public void sayHello() {<\/p>\n<p>        System.out.println(&quot;Hello from Java!&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u4e2d\u4f7f\u7528JPype\u8c03\u7528\u8fd9\u4e2aJava\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jpype<\/p>\n<h2><strong>\u542f\u52a8JVM<\/strong><\/h2>\n<p>jpype.startJVM(classpath=[&quot;path\/to\/your\/java\/classes&quot;])<\/p>\n<h2><strong>\u5bfc\u5165Java\u7c7b<\/strong><\/h2>\n<p>HelloWorld = jpype.JClass(&quot;HelloWorld&quot;)<\/p>\n<h2><strong>\u521b\u5efaJava\u5bf9\u8c61\u5e76\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>hello = HelloWorld()<\/p>\n<p>hello.sayHello()<\/p>\n<h2><strong>\u5173\u95edJVM<\/strong><\/h2>\n<p>jpype.shutdownJVM()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>classpath<\/code>\u53c2\u6570\u9700\u8981\u88ab\u8bbe\u7f6e\u4e3aJava\u7c7b\u6587\u4ef6\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>JPype\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1aJPype\u5141\u8bb8\u76f4\u63a5\u8c03\u7528Java\u7c7b\u548c\u65b9\u6cd5\uff0c\u5177\u6709\u8f83\u4f4e\u7684\u8c03\u7528\u5f00\u9500\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u9700\u8981\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u8fd0\u884cPython\u548cJava\u4ee3\u7801\uff0c\u5e76\u4e14\u5bf9JNI\u7684\u4f9d\u8d56\u53ef\u80fd\u4f1a\u5bfc\u81f4\u67d0\u4e9b\u5e73\u53f0\u4e0a\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PY4J\u5de5\u5177<\/p>\n<\/p>\n<p><p>Py4J\u662f\u4e00\u4e2a\u5141\u8bb8Python\u7a0b\u5e8f\u4e0eJava\u865a\u62df\u673a\uff08JVM\uff09\u4e0a\u7684Java\u5bf9\u8c61\u8fdb\u884c\u4e92\u64cd\u4f5c\u7684\u5de5\u5177\u3002\u5b83\u901a\u8fc7\u5728Python\u548cJava\u4e4b\u95f4\u5efa\u7acb\u4e00\u4e2aSocket\u8fde\u63a5\u5b9e\u73b0\u901a\u4fe1\u3002<\/p>\n<\/p>\n<ol>\n<li>Py4J\u5b89\u88c5\u4e0e\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u901a\u8fc7pip\u5b89\u88c5Py4J\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install py4j<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Java\u7a0b\u5e8f\uff0c\u5e76\u5728\u5176\u4e2d\u542f\u52a8Py4J\u7f51\u5173\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import py4j.GatewayServer;<\/p>\n<p>public class HelloWorld {<\/p>\n<p>    public void sayHello() {<\/p>\n<p>        System.out.println(&quot;Hello from Java!&quot;);<\/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>        HelloWorld app = new HelloWorld();<\/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><p>\u5728Python\u7aef\uff0c\u901a\u8fc7Py4J\u8fde\u63a5\u5230Java\u7a0b\u5e8f\u5e76\u8c03\u7528\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from py4j.java_gateway import JavaGateway<\/p>\n<h2><strong>\u8fde\u63a5\u5230Java\u7f51\u5173<\/strong><\/h2>\n<p>gateway = JavaGateway()<\/p>\n<h2><strong>\u83b7\u53d6Java\u5bf9\u8c61\u5e76\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>hello = gateway.entry_point<\/p>\n<p>hello.sayHello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>Py4J\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1aPy4J\u80fd\u591f\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\u8fd0\u884cPython\u548cJava\u4ee3\u7801\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u7f51\u7edc\u901a\u4fe1\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a\u7531\u4e8e\u901a\u4fe1\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\uff0c\u53ef\u80fd\u4f1a\u6709\u4e00\u5b9a\u7684\u6027\u80fd\u5f00\u9500\u3002\u6b64\u5916\uff0c\u9700\u8981\u5728Java\u7aef\u663e\u5f0f\u5730\u542f\u52a8\u7f51\u5173\u670d\u52a1\u5668\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001JYTHON\u5de5\u5177<\/p>\n<\/p>\n<p><p>Jython\u662fPython\u8bed\u8a00\u5728Java\u5e73\u53f0\u4e0a\u7684\u5b9e\u73b0\uff0c\u5141\u8bb8Python\u4ee3\u7801\u76f4\u63a5\u8c03\u7528Java\u7c7b\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>Jython\u5b89\u88c5\u4e0e\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u4eceJython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7dJython\u5e76\u8fdb\u884c\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Jython\u89e3\u91ca\u5668\u8fd0\u884cPython\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Java\u7c7b\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">public class HelloWorld {<\/p>\n<p>    public void sayHello() {<\/p>\n<p>        System.out.println(&quot;Hello from Java!&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f7f\u7528Jython\u89e3\u91ca\u5668\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from HelloWorld import HelloWorld<\/p>\n<h2><strong>\u521b\u5efaJava\u5bf9\u8c61\u5e76\u8c03\u7528\u65b9\u6cd5<\/strong><\/h2>\n<p>hello = HelloWorld()<\/p>\n<p>hello.sayHello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>Jython\u7684\u4f18\u7f3a\u70b9<\/li>\n<\/ol>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1aJython\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Java\u7c7b\u548c\u65b9\u6cd5\uff0c\u4ee3\u7801\u96c6\u6210\u7d27\u5bc6\u3002<\/p>\n<\/p>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1aJython\u4e0d\u652f\u6301C\u6269\u5c55\u548c\u67d0\u4e9bPython\u6a21\u5757\uff0c\u53ef\u80fd\u4e0d\u9002\u5408\u9700\u8981\u5b8c\u6574Python\u529f\u80fd\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u6839\u636e\u9879\u76ee\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u662f\u975e\u5e38\u91cd\u8981\u7684\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5982\u679c\u9879\u76ee\u9700\u8981\u76f4\u63a5\u8c03\u7528Java\u4ee3\u7801\u4e14\u8fd0\u884c\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\uff0cJPype\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/li>\n<li>\u5982\u679c\u9879\u76ee\u9700\u8981\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\u8fd0\u884c\uff0c\u6216\u8005\u9700\u8981\u5728Java\u548cPython\u4e4b\u95f4\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\uff0cPy4J\u53ef\u80fd\u66f4\u9002\u5408\u3002<\/li>\n<li>\u5982\u679c\u9879\u76ee\u4e0d\u4f9d\u8d56\u4e8eC\u6269\u5c55\u6a21\u5757\u4e14\u5e0c\u671b\u5728JVM\u4e0a\u8fd0\u884c\uff0cJython\u53ef\u80fd\u662f\u5408\u9002\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u4e0eJava\u7684\u4e92\u64cd\u4f5c\u6027\u5de5\u5177\u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u9009\u62e9\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u96c6\u6210Python\u548cJava\u4ee3\u7801\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u5347\u9879\u76ee\u7684\u5f00\u53d1\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027<\/strong>\u3002\u4e86\u89e3\u5404\u4e2a\u5de5\u5177\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u6709\u52a9\u4e8e\u505a\u51fa\u660e\u667a\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528Java\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8c03\u7528Java\u4ee3\u7801\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528JPype\u6216Py4J\u7b49\u5e93\u3002\u8fd9\u4e9b\u5e93\u5141\u8bb8Python\u7a0b\u5e8f\u4e0eJava\u865a\u62df\u673a\u8fdb\u884c\u4ea4\u4e92\uff0c\u4ece\u800c\u8c03\u7528Java\u7c7b\u548c\u65b9\u6cd5\u3002\u60a8\u53ea\u9700\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u542f\u52a8Java\u865a\u62df\u673a\uff0c\u5e76\u901a\u8fc7\u7b80\u5355\u7684Python\u4ee3\u7801\u8c03\u7528\u6240\u9700\u7684Java\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u8c03\u7528Java\u4ee3\u7801\u65f6\u6709\u54ea\u4e9b\u6027\u80fd\u5f71\u54cd\uff1f<\/strong><br \/>\u5728\u8c03\u7528Java\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u4e00\u5b9a\u5f71\u54cd\uff0c\u4e3b\u8981\u662f\u7531\u4e8e\u8de8\u8bed\u8a00\u8c03\u7528\u7684\u5f00\u9500\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u5efa\u8bae\u5728Python\u548cJava\u4e4b\u95f4\u8fdb\u884c\u6279\u91cf\u6570\u636e\u4f20\u8f93\uff0c\u800c\u4e0d\u662f\u9010\u4e2a\u5bf9\u8c61\u8fdb\u884c\u4ea4\u4e92\u3002\u6b64\u5916\uff0c\u5c3d\u91cf\u51cf\u5c11\u9891\u7e41\u7684Java\u8c03\u7528\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u591a\u4e2a\u64cd\u4f5c\u5c01\u88c5\u5728Java\u65b9\u6cd5\u4e2d\u6765\u4f18\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Python\u548cJava\u4e4b\u95f4\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1f<\/strong><br \/>Python\u548cJava\u4e4b\u95f4\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u5927\u591a\u6570\u5e93\u63d0\u4f9b\u4e86\u81ea\u52a8\u7c7b\u578b\u8f6c\u6362\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u624b\u52a8\u8fdb\u884c\u8f6c\u6362\u3002\u4f8b\u5982\uff0cPython\u4e2d\u7684\u5217\u8868\u53ef\u4ee5\u8f6c\u6362\u4e3aJava\u4e2d\u7684\u6570\u7ec4\uff0c\u800cPython\u7684\u5b57\u5178\u53ef\u4ee5\u6620\u5c04\u5230Java\u7684Map\u3002\u4e86\u89e3\u8fd9\u4e9b\u8f6c\u6362\u89c4\u5219\u5c06\u6709\u52a9\u4e8e\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528Java\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528JPype\u3001Py4J\u6216Jython\u7b49\u5de5\u5177\u5b9e\u73b0\uff0c\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u4f18\u52a3\uff0c\u53ef\u4ee5\u6839 [&hellip;]","protected":false},"author":3,"featured_media":988796,"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\/988780"}],"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=988780"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988780\/revisions"}],"predecessor-version":[{"id":988797,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/988780\/revisions\/988797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/988796"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=988780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=988780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=988780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}