Java實現(xiàn)的執(zhí)行python腳本工具類示例【使用jython.jar】
本文實例講述了Java實現(xiàn)的執(zhí)行python腳本工具類。分享給大家供大家參考,具體如下:
這里java中執(zhí)行python腳本工具類,需要使用jython.jar
java中執(zhí)行python腳本工具類,學(xué)習(xí)的時候?qū)懼妫?/p>
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public final class JythonUtil {
private JythonUtil(){}
/**
* 執(zhí)行某個.py文件
* @param filePath
* @throws IOException
*/
public static void pythonExecute(String filePath) throws IOException{
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
pin.execfile(is);
is.close();
}
/**
* 獲取python程序的變量值
* @param filePath
* @param ponames
* @return
* @throws IOException
*/
public static List<PyObject> transP2JData(String filePath, String...ponames) throws IOException{
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
pin.execfile(is);
is.close();
List<PyObject> pos = new ArrayList<>();
for (String poname : ponames) {
PyObject po = pin.get(poname);
pos.add(po);
}
return pos;
}
/**
* 將參數(shù)賦給python程序執(zhí)行
* @param filePath
* @param pomaps
* @throws IOException
*/
public static void transJ2PData(String filePath, Map<String, Object> pomaps) throws IOException {
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
for (String pomapkey : pomaps.keySet()) {
pin.set(pomapkey, pomaps.get(pomapkey));
}
pin.execfile(is);
is.close();
}
/**
* 將參數(shù)賦給python程序執(zhí)行,并獲取python中的變量值
* @param filePath
* @param pomaps
* @param ponames
* @return
* @throws IOException
*/
public static List<PyObject> transJ2PData(String filePath, Map<String, Object> pomaps, String...ponames) throws IOException {
PythonInterpreter pin = new PythonInterpreter();
InputStream is = new FileInputStream(filePath);
for (String pomapkey : pomaps.keySet()) {
pin.set(pomapkey, pomaps.get(pomapkey));
}
pin.execfile(is);
is.close();
List<PyObject> pos = new ArrayList<>();
for (String poname : ponames) {
PyObject po = pin.get(poname);
pos.add(po);
}
return pos;
}
}
附:jython.jar點(diǎn)擊此處本站下載。
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
Python使用Paramiko模塊編寫腳本進(jìn)行遠(yuǎn)程服務(wù)器操作
這篇文章主要介紹了Python使用Paramiko模塊編寫腳本進(jìn)行遠(yuǎn)程服務(wù)器操作的實例,通過Paramiko能夠方便地使用SSH服務(wù),需要的朋友可以參考下2016-05-05
利用Python+Selenium破解春秋航空網(wǎng)滑塊驗證碼的實戰(zhàn)過程
本文給大家介紹使用Python+Selenium破解春秋航空網(wǎng)滑塊驗證碼的實戰(zhàn)過程,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-08-08
python人工智能算法之人工神經(jīng)網(wǎng)絡(luò)
這篇文章主要為大家介紹了python人工智能算法之人工神經(jīng)網(wǎng)絡(luò)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Python實現(xiàn)給qq郵箱發(fā)送郵件的方法
這篇文章主要介紹了Python實現(xiàn)給qq郵箱發(fā)送郵件的方法,涉及Python郵件發(fā)送的相關(guān)技巧,需要的朋友可以參考下2015-05-05

