亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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腳本的方法。分享給大家供大家參考,具體如下:

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

1、在Eclipse中新建Maven工程;

2、pom.xml文件中添加如下依賴包之后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、編寫如下測試代碼;

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、測試:

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

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、問題解決:

重構(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、解決問題參考:

http://bugs.jython.org/issue2355

補(bǔ)充:jpython拋錯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ù)雜類接受處理
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)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

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

相關(guān)文章

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

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

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

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

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

    SpringBoot淺析安全管理之Shiro框架

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

    java爬取豆瓣電影示例解析

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

    詳解Java中Period類的使用方法

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

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

    Thread線程類自帶的stop方法,但是jdk不建議使用,因?yàn)閟top方法終止線程只是強(qiáng)行終止,內(nèi)存中部分值可能已發(fā)生變化,并未保證數(shù)據(jù)的一致性,將會導(dǎo)致線程安全問題,那么在什么情況下可以終止線程呢,本篇帶你探究一下
    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í)獲取和操作類的內(nèi)部信息,提高了代碼的靈活度,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • 5個并發(fā)處理技巧代碼示例

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

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

    Java全排列算法字典序下的下一個排列講解

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

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

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

最新評論