国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java實現調用jython執行python文件的方法

2024-07-14 08:39:54
字體:
來源:轉載
供稿:網友

本文實例講述了Java實現調用jython執行python文件的方法。分享給大家供大家參考,具體如下:

在web開發時候,經常在web環境使用本地環境的第三方庫什么的,本文講解java如何執行python文件。

網上說方法有三種,其實也就兩種,下面著中介紹第二種通過(jython)。

方法一

java.lang.Runtime  Runtime rt = Runtime.getRuntime();    try {        Process proc = rt.exec("python /tmp/test.py");    }catch (Exception e){      e.printStackTrace();    }

小計一下:

1、Runtime.getRuntime()可以取得當前JVM的運行時環境,這也是在Java中唯一一個得到運行時環境的方法。

2、Runtime上其他大部分的方法都是實例方法,也就是說每次進行運行時調用時都要用到getRuntime方法。

3、Runtime中的exit方法是退出當前JVM的方法,估計也是唯一的一個吧,因為我看到System類中的exit實際上也是通過調用Runtime.exit()來退出JVM的,這里說明一下Java對Runtime返回值的一般規則(后邊也提到了),0代表正常退出,非0代表異常中止,這只是Java的規則,在各個操作系統中總會發生一些小的混淆。

第二種(重點)

調用jython API

第一步:添加依賴

<!-- https://mvnrepository.com/artifact/org.python/jython -->  <dependency>   <groupId>org.python</groupId>   <artifactId>jython</artifactId>   <version>2.7.0</version>  </dependency>

第二步:新建一個Test.java測試類

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'");  }}

第三步:運行Test.java

Testing started at 下午9:40 ...Tuethis is test.pycreated by tengxing on 2017.3!

進程已結束,退出代碼0

提醒可能報如下異常:

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 完美

//調用python中的方法,并且打印結果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());

附:jython.jar點擊此處本站下載

希望本文所述對大家java程序設計有所幫助。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 来安县| 西平县| 梧州市| 泽州县| 洞口县| 从江县| 丹阳市| 青岛市| 商水县| 东辽县| 鄂托克前旗| 永吉县| 鸡东县| 刚察县| 大余县| 延川县| 东乡| 津南区| 巴东县| 石屏县| 沁水县| 遂川县| 肇源县| 文昌市| 邹城市| 赤壁市| 社会| 翁源县| 岳普湖县| 松江区| 文成县| 苗栗市| 乌兰察布市| 洛阳市| 永寿县| 正镶白旗| 平阳县| 望谟县| 邯郸市| 嘉善县| 精河县|