這篇文章將為大家詳細(xì)講解有關(guān)如何使用Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
站在用戶的角度思考問題,與客戶深入溝通,找到宣州網(wǎng)站設(shè)計(jì)與宣州網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋宣州地區(qū)。
具體如下:
在web開發(fā)時(shí)候,經(jīng)常在web環(huán)境使用本地環(huán)境的第三方庫什么的,本文講解java如何執(zhí)行python文件。
網(wǎng)上說方法有三種,其實(shí)也就兩種,下面著中介紹第二種通過(jython)。
方法一
java.lang.Runtime Runtime rt = Runtime.getRuntime(); try { Process proc = rt.exec("python /tmp/test.py"); }catch (Exception e){ e.printStackTrace(); }
小計(jì)一下:
1、Runtime.getRuntime()
可以取得當(dāng)前JVM的運(yùn)行時(shí)環(huán)境,這也是在Java中唯一一個(gè)得到運(yùn)行時(shí)環(huán)境的方法。
2、Runtime上其他大部分的方法都是實(shí)例方法,也就是說每次進(jìn)行運(yùn)行時(shí)調(diào)用時(shí)都要用到getRuntime
方法。
3、Runtime中的exit方法是退出當(dāng)前JVM的方法,估計(jì)也是唯一的一個(gè)吧,因?yàn)槲铱吹絊ystem類中的exit實(shí)際上也是通過調(diào)用Runtime.exit()
來退出JVM的,這里說明一下Java對(duì)Runtime返回值的一般規(guī)則(后邊也提到了),0代表正常退出,非0代表異常中止,這只是Java的規(guī)則,在各個(gè)操作系統(tǒng)中總會(huì)發(fā)生一些小的混淆。
第二種(重點(diǎn))
調(diào)用jython API
第一步:添加依賴
<!-- https://mvnrepository.com/artifact/org.python/jython --> <dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.0</version> </dependency>
第二步:新建一個(gè)Test.java測(cè)試類
import org.python.util.PythonInterpreter; import java.util.Properties; /** * Author: 遇見小星 * Email: tengxing7452@163.com * Date: 17-3-21 * Time: 下午8:18 * Describe: jpython test */ public class Test { public static void main(String []args){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('Mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); interpreter.execfile("/tmp/test.py"); interpreter.exec("print 'created by tengxing on 2017.3'"); } }
第三步:運(yùn)行Test.java
Testing started at 下午9:40 ... Tue this is test.py created by tengxing on 2017.3!
進(jìn)程已結(jié)束,退出代碼0
提醒可能報(bào)如下異常:
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
原因:沒有初始化 python.import.site
解決:
public class Test { public static void main(String []args){ Properties props = new Properties(); props.put("python.home", "path to the Lib folder"); props.put("python.console.encoding", "UTF-8"); props.put("python.security.respectJavaAccessibility", "false"); props.put("python.import.site", "false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); interpreter.exec("print days[1];"); interpreter.execfile("/tmp/test.py"); interpreter.exec("print 'created by tengxing on 2017.3!'"); } }
ok 完美
//調(diào)用python中的方法,并且打印結(jié)果 PyFunction func = (PyFunction) interpreter.get("adder",PyFunction.class); int a = 2010, b = 2; PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b)); System.out.println("anwser = " + pyobj.toString());
關(guān)于“如何使用Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
本文標(biāo)題:如何使用Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件
網(wǎng)址分享:http://aaarwkj.com/article46/gdegeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、、響應(yīng)式網(wǎng)站、服務(wù)器托管、網(wǎng)頁設(shè)計(jì)公司、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)