最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java程序中實(shí)現(xiàn)調(diào)用Python腳本的方法詳解

 更新時(shí)間:2018年03月29日 10:55:04   作者:EliteQing  
這篇文章主要介紹了Java程序中實(shí)現(xiàn)調(diào)用Python腳本的方法,結(jié)合實(shí)例形式分析了eclipse環(huán)境中使用Java調(diào)用Python腳本的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Java程序中實(shí)現(xiàn)調(diào)用Python腳本的方法。分享給大家供大家參考,具體如下:

在程序開(kāi)發(fā)中,有時(shí)候需要Java程序中調(diào)用相關(guān)Python腳本,以下內(nèi)容記錄了先關(guān)步驟和可能出現(xiàn)問(wèn)題的解決辦法。

1、在Eclipse中新建Maven工程;

2、pom.xml文件中添加如下依賴(lài)包之后update maven工程;

<dependency>
  <groupId>org.python</groupId>
  <artifactId>jython</artifactId>
  <version>2.7.0</version>
</dependency>
<dependency>
  <groupId>org.python</groupId>
  <artifactId>jython-standalone</artifactId>
  <version>2.7.0</version>
</dependency>

3、編寫(xiě)如下測(cè)試代碼;

import org.python.util.PythonInterpreter;
public class JpythonScript {
 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];");
 }
}

4、測(cè)試:

出現(xiàn)如下錯(cuò)誤:

console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
  * sys.path: ['...python\\jython\\2.7.0\\Lib', '__classpath__', '__pyclasspath__/']
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix:***\jython\2.7.0
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file
You can use the -S option or python.import.site=false to not import the site module

5、問(wèn)題解決:

重構(gòu)代碼如下:

import java.util.Properties;
import org.python.util.PythonInterpreter;
public class JpythonScript {
 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];");
 }
}

6、編譯成功。

7、解決問(wèn)題參考:

http://bugs.jython.org/issue2355

補(bǔ)充:jpython拋錯(cuò)Cannot import site module的解決方法

Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site

public class JpythonScript {
  public static void main(String args[]) {
    Properties props = new Properties();
    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];");
  }

// 進(jìn)行復(fù)雜類(lèi)接受處理
Map<String, Object> res = new HashMap<String, Object>();
res.put("1", "Danny");
res.put("2", "Fanny");
PythonInterpreter interpM = new PythonInterpreter();
interpM.execfile("./src/com/DataDeal.py");
PyFunction pyFunctionM = (PyFunction) interpM.get("main", PyFunction.class);
Map<PyObject, PyObject> tableM = new HashMap<PyObject, PyObject>();
tableM.put(new PyString("conf"), PyJavaType.wrapJavaObject(res));
PyDictionary pydM = new PyDictionary(tableM);

更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • SpringBoot啟動(dòng)后啟動(dòng)內(nèi)嵌瀏覽器的方法

    SpringBoot啟動(dòng)后啟動(dòng)內(nèi)嵌瀏覽器的方法

    這篇文章主要介紹了SpringBoot啟動(dòng)后啟動(dòng)內(nèi)嵌瀏覽器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • selenium-java實(shí)現(xiàn)自動(dòng)登錄跳轉(zhuǎn)頁(yè)面方式

    selenium-java實(shí)現(xiàn)自動(dòng)登錄跳轉(zhuǎn)頁(yè)面方式

    利用Selenium和Java語(yǔ)言可以編寫(xiě)一個(gè)腳本自動(dòng)刷新網(wǎng)頁(yè),首先,需要確保Google瀏覽器和Chrome-Driver驅(qū)動(dòng)的版本一致,通過(guò)指定網(wǎng)站下載對(duì)應(yīng)版本的瀏覽器和驅(qū)動(dòng),在Maven項(xiàng)目中添加依賴(lài),編寫(xiě)腳本實(shí)現(xiàn)網(wǎng)頁(yè)的自動(dòng)刷新,此方法適用于需要頻繁刷新網(wǎng)頁(yè)的場(chǎng)景,簡(jiǎn)化了操作,提高了效率
    2024-11-11
  • SpringBoot淺析安全管理之Shiro框架

    SpringBoot淺析安全管理之Shiro框架

    安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會(huì)發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會(huì)出現(xiàn)問(wèn)題,這篇文章主要介紹了SpringBoot安全管理Shiro框架的使用
    2022-08-08
  • java爬取豆瓣電影示例解析

    java爬取豆瓣電影示例解析

    這篇文章主要介紹了java爬取豆瓣電影示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 詳解Java中Period類(lèi)的使用方法

    詳解Java中Period類(lèi)的使用方法

    Period類(lèi)通過(guò)年、月、日相結(jié)合來(lái)描述一個(gè)時(shí)間量,最高精度是天。本文將通過(guò)示例詳細(xì)為大家講講Period類(lèi)的使用,需要的可以參考一下
    2022-05-05
  • Java線程在什么情況下可以終止

    Java線程在什么情況下可以終止

    Thread線程類(lèi)自帶的stop方法,但是jdk不建議使用,因?yàn)閟top方法終止線程只是強(qiáng)行終止,內(nèi)存中部分值可能已發(fā)生變化,并未保證數(shù)據(jù)的一致性,將會(huì)導(dǎo)致線程安全問(wèn)題,那么在什么情況下可以終止線程呢,本篇帶你探究一下
    2022-04-04
  • 快速掌握J(rèn)ava中注解與反射

    快速掌握J(rèn)ava中注解與反射

    本文詳細(xì)介紹了Java中注解和反射的概念及應(yīng)用,注解是用于給代碼添加元數(shù)據(jù)的標(biāo)記,如@Override、@Deprecated等,反射機(jī)制則是在運(yùn)行時(shí)獲取和操作類(lèi)的內(nèi)部信息,提高了代碼的靈活度,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • 5個(gè)并發(fā)處理技巧代碼示例

    5個(gè)并發(fā)處理技巧代碼示例

    這篇文章主要介紹了5個(gè)并發(fā)處理技巧代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Java全排列算法字典序下的下一個(gè)排列講解

    Java全排列算法字典序下的下一個(gè)排列講解

    今天小編就為大家分享一篇關(guān)于Java全排列字典序下的下一個(gè)排列,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • java讀寫(xiě)串口數(shù)據(jù)你了解多少

    java讀寫(xiě)串口數(shù)據(jù)你了解多少

    這篇文章主要為大家詳細(xì)介紹了java讀寫(xiě)串口數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02

最新評(píng)論

吉安县| 安图县| 新田县| 左权县| 乌鲁木齐县| 衡山县| 万安县| 逊克县| 于田县| 开鲁县| 西和县| 丽江市| 咸宁市| 介休市| 永嘉县| 苗栗县| 商水县| 邯郸县| 岑巩县| 张家界市| 日喀则市| 莱州市| 永寿县| 连城县| 重庆市| 镇赉县| 平果县| 视频| 社旗县| 博野县| 高密市| 仪陇县| 房产| 肇源县| 宜阳县| 彭州市| 开原市| 厦门市| 莱西市| 荔浦县| 布拖县|