{"id":990121,"date":"2024-12-27T08:17:32","date_gmt":"2024-12-27T00:17:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/990121.html"},"modified":"2024-12-27T08:17:35","modified_gmt":"2024-12-27T00:17:35","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9java%e6%89%93%e5%bc%80python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/990121.html","title":{"rendered":"\u5982\u4f55\u8ba9java\u6253\u5f00Python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065016\/f46ed1d2-226e-435a-bf6d-09cf4cd39e89.webp\" alt=\"\u5982\u4f55\u8ba9java\u6253\u5f00Python\" \/><\/p>\n<p><p> <strong>\u8981\u8ba9Java\u6253\u5f00Python\uff0c\u53ef\u4ee5\u4f7f\u7528Java ProcessBuilder\u3001Jython\u3001Java Scripting API\u3002<\/strong>\u5176\u4e2d\uff0c<strong>ProcessBuilder<\/strong> \u662f\u4e00\u79cd\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u7cfb\u7edf\u547d\u4ee4\u5e76\u4e0ePython\u811a\u672c\u4ea4\u4e92\u3002\u5728\u8fd9\u79cd\u65b9\u6cd5\u4e2d\uff0cJava\u7a0b\u5e8f\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\u6765\u8fd0\u884cPython\u89e3\u91ca\u5668\uff0c\u5e76\u5c06Python\u811a\u672c\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u8be5\u89e3\u91ca\u5668\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u7b80\u5355\u6027\u548c\u7075\u6d3b\u6027\uff0c\u56e0\u4e3a\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u4efb\u4f55Python\u811a\u672c\uff0c\u800c\u4e0d\u9700\u8981\u5bf9Java\u548cPython\u4e4b\u95f4\u7684\u63a5\u53e3\u8fdb\u884c\u590d\u6742\u7684\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u5173\u4e8e\u5982\u4f55\u4f7f\u7528Java\u6253\u5f00Python\u7684\u8be6\u7ec6\u8bf4\u660e\uff1a<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528ProcessBuilder<\/strong><\/h2>\n<p><p>ProcessBuilder \u662fJava\u4e2d\u4e00\u4e2a\u7528\u4e8e\u542f\u52a8\u548c\u7ba1\u7406\u8fdb\u7a0b\u7684\u7c7b\u3002\u901a\u8fc7\u5b83\u53ef\u4ee5\u5728Java\u7a0b\u5e8f\u4e2d\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u521b\u5efa\u5e76\u542f\u52a8\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>ProcessBuilder<\/code>\u5bf9\u8c61\uff0c\u5e76\u5c06Python\u89e3\u91ca\u5668\u7684\u8def\u5f84\u548cPython\u811a\u672c\u7684\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5b83\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>start()<\/code>\u65b9\u6cd5\u6765\u542f\u52a8\u8be5\u8fdb\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import java.io.*;<\/p>\n<p>public class JavaToPython {<\/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>        try {<\/p>\n<p>            \/\/ \u521b\u5efaProcessBuilder\u5bf9\u8c61\u5e76\u4f20\u5165Python\u89e3\u91ca\u5668\u548c\u811a\u672c\u8def\u5f84<\/p>\n<p>            ProcessBuilder pb = new ProcessBuilder(&quot;python&quot;, &quot;path\/to\/script.py&quot;);<\/p>\n<p>            \/\/ \u542f\u52a8\u8fdb\u7a0b<\/p>\n<p>            Process process = pb.start();<\/p>\n<p>            \/\/ \u8bfb\u53d6Python\u811a\u672c\u7684\u8f93\u51fa<\/p>\n<p>            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));<\/p>\n<p>            String line;<\/p>\n<p>            while ((line = in.readLine()) != null) {<\/p>\n<p>                System.out.println(line);<\/p>\n<p>            }<\/p>\n<p>        } catch (IOException e) {<\/p>\n<p>            e.printStackTrace();<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u5904\u7406\u8f93\u5165\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4e0ePython\u811a\u672c\u8fdb\u884c\u4ea4\u4e92\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u8f93\u5165\u8f93\u51fa\u6d41\u3002\u53ef\u4ee5\u901a\u8fc7<code>Process<\/code>\u5bf9\u8c61\u7684<code>getOutputStream()<\/code>\u3001<code>getInputStream()<\/code>\u548c<code>getErrorStream()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">\/\/ \u5411Python\u811a\u672c\u4f20\u9012\u8f93\u5165<\/p>\n<p>OutputStream os = process.getOutputStream();<\/p>\n<p>os.write(&quot;input data\\n&quot;.getBytes());<\/p>\n<p>os.flush();<\/p>\n<p>\/\/ \u8bfb\u53d6Python\u811a\u672c\u7684\u8f93\u51fa<\/p>\n<p>BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));<\/p>\n<p>BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.3 \u5904\u7406\u5f02\u5e38\u548c\u9519\u8bef<\/h3>\n<\/p>\n<p><p>\u5728\u8fd0\u884cPython\u811a\u672c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f02\u5e38\u548c\u9519\u8bef\u3002\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u9519\u8bef\u6d41\u6765\u6355\u83b7\u8fd9\u4e9b\u4fe1\u606f\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">String s;<\/p>\n<p>while ((s = stdError.readLine()) != null) {<\/p>\n<p>    System.err.println(s);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528Jython<\/strong><\/h2>\n<p><p>Jython\u662fPython\u7684Java\u5b9e\u73b0\uff0c\u5b83\u5141\u8bb8\u5728Java\u7a0b\u5e8f\u4e2d\u76f4\u63a5\u5d4c\u5165Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u5b89\u88c5Jython<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u4e0b\u8f7d\u5e76\u5b89\u88c5Jython\u3002\u53ef\u4ee5\u4eceJython\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u6700\u65b0\u7248\u672c\u7684Jython\u5e76\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u4f7f\u7528Jython\u8fd0\u884cPython\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7Jython\u7684<code>org.python.util.PythonInterpreter<\/code>\u7c7b\u6765\u8fd0\u884cPython\u811a\u672c\u3002<\/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>        \/\/ \u8fd0\u884cPython\u811a\u672c<\/p>\n<p>        interpreter.execfile(&quot;path\/to\/script.py&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3 \u4e0eJava\u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/h3>\n<\/p>\n<p><p>Jython\u5141\u8bb8\u5728Java\u548cPython\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002\u53ef\u4ee5\u901a\u8fc7<code>set()<\/code>\u65b9\u6cd5\u5c06Java\u5bf9\u8c61\u4f20\u9012\u7ed9Python\u811a\u672c\uff0c\u6216\u901a\u8fc7<code>eval()<\/code>\u65b9\u6cd5\u4ecePython\u811a\u672c\u4e2d\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">interpreter.set(&quot;variable&quot;, value);<\/p>\n<p>interpreter.exec(&quot;result = some_python_function(variable)&quot;);<\/p>\n<p>PyObject result = interpreter.get(&quot;result&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528Java Scripting API<\/strong><\/h2>\n<p><p>Java\u63d0\u4f9b\u4e86\u4e00\u4e2a\u811a\u672cAPI\uff0c\u5141\u8bb8\u5728Java\u7a0b\u5e8f\u4e2d\u8fd0\u884c\u591a\u79cd\u811a\u672c\u8bed\u8a00\uff0c\u5305\u62ecPython\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u914d\u7f6e\u811a\u672c\u5f15\u64ce<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u83b7\u53d6\u811a\u672c\u5f15\u64ce\u7ba1\u7406\u5668\uff0c\u5e76\u901a\u8fc7\u5b83\u6765\u83b7\u53d6Python\u811a\u672c\u5f15\u64ce\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">import javax.script.*;<\/p>\n<p>public class ScriptEngineExample {<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        ScriptEngineManager manager = new ScriptEngineManager();<\/p>\n<p>        ScriptEngine engine = manager.getEngineByName(&quot;python&quot;);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u6267\u884cPython\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>ScriptEngine<\/code>\u5bf9\u8c61\u7684<code>eval()<\/code>\u65b9\u6cd5\u6765\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">try {<\/p>\n<p>    engine.eval(new FileReader(&quot;path\/to\/script.py&quot;));<\/p>\n<p>} catch (ScriptException | FileNotFoundException e) {<\/p>\n<p>    e.printStackTrace();<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.3 \u4f20\u9012\u53d8\u91cf\u548c\u83b7\u53d6\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>put()<\/code>\u65b9\u6cd5\u5c06Java\u53d8\u91cf\u4f20\u9012\u7ed9Python\u811a\u672c\uff0c\u6216\u901a\u8fc7<code>eval()<\/code>\u65b9\u6cd5\u83b7\u53d6Python\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">engine.put(&quot;variable&quot;, value);<\/p>\n<p>Object result = engine.eval(&quot;some_python_function(variable)&quot;);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p><h3>4.1 \u8003\u8651\u9879\u76ee\u9700\u6c42<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5982\u4f55\u8ba9Java\u6253\u5f00Python\u65f6\uff0c\u9700\u8981\u8003\u8651\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u662f\u5426\u9700\u8981\u4e0ePython\u811a\u672c\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u4ea4\u6362\uff0c\u6216\u8005\u662f\u5426\u9700\u8981\u5728Java\u4e2d\u76f4\u63a5\u5d4c\u5165Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u8003\u8651\u6280\u672f\u9650\u5236<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u65b9\u6cd5\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u6280\u672f\u9650\u5236\u3002\u4f8b\u5982\uff0cProcessBuilder\u9700\u8981\u5728\u7cfb\u7edf\u73af\u5883\u4e2d\u5b89\u88c5Python\u89e3\u91ca\u5668\uff0c\u800cJython\u53ef\u80fd\u4e0d\u652f\u6301Python\u7684\u6240\u6709\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h3>4.3 \u8003\u8651\u6027\u80fd\u548c\u53ef\u7ef4\u62a4\u6027<\/h3>\n<\/p>\n<p><p>\u9700\u8981\u8003\u8651\u4e0d\u540c\u65b9\u6cd5\u7684\u6027\u80fd\u548c\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002\u4f8b\u5982\uff0cProcessBuilder\u7684\u6027\u80fd\u53ef\u80fd\u4e0d\u5982Jython\uff0c\u4f46\u4ee3\u7801\u66f4\u7b80\u5355\u3001\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c<strong>\u4f7f\u7528ProcessBuilder\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\uff0c\u4f46\u5982\u679c\u9879\u76ee\u9700\u8981\u9891\u7e41\u7684Java-Python\u4ea4\u4e92\u6216\u8005\u5e0c\u671b\u5c06Python\u4ee3\u7801\u5d4c\u5165Java\u4e2d\uff0c\u90a3\u4e48<strong>Jython\u6216Java Scripting API\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Java\u4e2d\u8c03\u7528Python\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728Java\u4e2d\u8c03\u7528Python\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528Java\u7684ProcessBuilder\u7c7b\u6765\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u6267\u884cPython\u89e3\u91ca\u5668\u5e76\u4f20\u9012\u811a\u672c\u7684\u8def\u5f84\u3002\u786e\u4fdd\u5728\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u6b63\u786e\u8bbe\u7f6e\u4e86Python\u7684\u8def\u5f84\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">ProcessBuilder processBuilder = new ProcessBuilder(&quot;python&quot;, &quot;your_script.py&quot;);\nprocessBuilder.redirectErrorStream(true);\nProcess process = processBuilder.start();\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cJava\u53ef\u4ee5\u4e0ePython\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p><strong>\u5728Java\u4e2d\u8c03\u7528Python\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4f9d\u8d56\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Java\u8c03\u7528Python\u811a\u672c\u65f6\uff0c\u786e\u4fddPython\u73af\u5883\u5df2\u7ecf\u5b89\u88c5\uff0c\u5e76\u4e14\u5728Java\u5e94\u7528\u7a0b\u5e8f\u4e2d\u80fd\u591f\u627e\u5230Python\u7684\u53ef\u6267\u884c\u6587\u4ef6\u3002\u5982\u679c\u9700\u8981\u7279\u5b9a\u7684Python\u5e93\uff0c\u786e\u4fdd\u8fd9\u4e9b\u5e93\u5df2\u7ecf\u5b89\u88c5\u5728Python\u73af\u5883\u4e2d\u3002\u4f7f\u7528\u865a\u62df\u73af\u5883\u4e5f\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\uff0c\u4ee5\u907f\u514d\u4f9d\u8d56\u51b2\u7a81\u3002<\/p>\n<p><strong>Java\u4e0ePython\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u5982\u4f55\u5b9e\u73b0\uff1f<\/strong><br \/>Java\u4e0ePython\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u6d41\u5b9e\u73b0\u3002Java\u53ef\u4ee5\u5c06\u6570\u636e\u901a\u8fc7Process\u7684OutputStream\u4f20\u9012\u7ed9Python\uff0c\u800cPython\u53ef\u4ee5\u901a\u8fc7sys.stdin\u8bfb\u53d6\u8fd9\u4e9b\u6570\u636e\u3002\u540c\u65f6\uff0cPython\u5904\u7406\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5c06\u7ed3\u679c\u901a\u8fc7OutputStream\u8fd4\u56de\u7ed9Java\uff0cJava\u5219\u53ef\u4ee5\u901a\u8fc7InputStream\u8bfb\u53d6\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6709\u6548\u7684\u53cc\u5411\u901a\u4fe1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba9Java\u6253\u5f00Python\uff0c\u53ef\u4ee5\u4f7f\u7528Java ProcessBuilder\u3001Jython\u3001Java Scri [&hellip;]","protected":false},"author":3,"featured_media":990128,"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\/990121"}],"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=990121"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990121\/revisions"}],"predecessor-version":[{"id":990131,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990121\/revisions\/990131"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/990128"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=990121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=990121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=990121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}