Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件的方法
本文實(shí)例講述了Java實(shí)現(xiàn)調(diào)用jython執(zhí)行python文件的方法。分享給大家供大家參考,具體如下:
在web開發(fā)時(shí)候,經(jīng)常在web環(huán)境使用本地環(huán)境的第三方庫(kù)什么的,本文講解java如何執(zhí)行python文件。
網(wǎng)上說(shuō)方法有三種,其實(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í)例方法,也就是說(shuō)每次進(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()來(lái)退出JVM的,這里說(shuō)明一下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());
參考文章:
http://www.dbjr.com.cn/article/137380.htm
http://www.dbjr.com.cn/article/137385.htm
附:jython.jar點(diǎn)擊此處本站下載。
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
淺析Java中SimpleDateFormat為什么是線程不安全的
SimpleDateFormat是Java中用于日期時(shí)間格式化的一個(gè)類,它提供了對(duì)日期的解析和格式化能力,本文主要來(lái)和大家一起探討一下SimpleDateFormat為什么是線程不安全的,感興趣的可以了解下2024-02-02
Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法
這篇文章主要介紹了Java實(shí)現(xiàn)求小于n的質(zhì)數(shù)的3種方法,本文給出了根據(jù)定義去求解、平方根、找規(guī)律三種解法,需要的朋友可以參考下2015-03-03
Java 通過位運(yùn)算求一個(gè)集合的所有子集方法
下面小編就為大家?guī)?lái)一篇Java 通過位運(yùn)算求一個(gè)集合的所有子集方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-03-03
WIN10環(huán)境 Maven的安裝與配置詳細(xì)教程
這篇文章主要介紹了WIN10環(huán)境 Maven的安裝與配置詳細(xì)教程,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Gateway網(wǎng)關(guān)自定義攔截器的不可重復(fù)讀取數(shù)據(jù)問題
這篇文章主要介紹了Gateway網(wǎng)關(guān)自定義攔截器的不可重復(fù)讀取數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Spring @Lookup深入分析實(shí)現(xiàn)原理
這篇文章主要介紹了Spring @Lookup實(shí)現(xiàn)原理,我們知道在spring容器中單獨(dú)的一個(gè)抽象類是不能成為一個(gè)bean的,那么有沒有辦法呢?這個(gè)時(shí)候我們可以使用Lookup注解2023-01-01
Spring Boot中單例類實(shí)現(xiàn)對(duì)象的注入方式
這篇文章主要介紹了Spring Boot中單例類實(shí)現(xiàn)對(duì)象的注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot返回多種格式的數(shù)據(jù)的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot返回多種格式的數(shù)據(jù)的實(shí)現(xiàn)示例,主要包括了FastJson,xml,pdf,excel,資源流,具有一定的參考價(jià)值,感興趣的可以了解一下2021-10-10

