python代碼如何轉(zhuǎn)jar包
一、安裝
首先需要安裝jython-installer-2.7.1的jar包工具。
二、注意需要默認(rèn)安裝
三、使用cmd進(jìn)入jython環(huán)境
3.1、配置環(huán)境變量
3.2、注冊(cè)環(huán)境點(diǎn)擊運(yùn)行jython_regrtest.bat
3.3、最后后使用命令進(jìn)行轉(zhuǎn)換,但是只有jython-installer-2.2.0
版本才能使用命令進(jìn)行轉(zhuǎn)換,由于jython-installer-2.7.0
的版本升級(jí),現(xiàn)在直接類(lèi)型配置文件或者寫(xiě)在run()或者main(
)方法中才能運(yùn)行。
引入pom.xml
<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.0</version> </dependency>
java代碼
package com.xxxx.checkandbigdataquery.jpython; import org.python.core.Py; import org.python.core.PySystemState; import org.python.util.PythonInterpreter; public class HelloPython { public static void main(String[] args) { test1(); //test2(); //test3(); } /** * 運(yùn)行python文件 */ public static void test1() { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("D:/jython2.7.1/test.py"); } /** * 運(yùn)行python 代碼 */ public static void test2(){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("print('hello')"); } /** * 手動(dòng)添加第三方庫(kù)路徑 * 把第三方庫(kù)文件夾放到執(zhí)行的.py腳本同級(jí)目錄 */ public static void test3(){ PySystemState sys = Py.getSystemState(); System.out.println(sys.path.toString()); sys.path.add("F:\\Python27\\Lib\\site-packages\\jieba"); } }
package com.xxxx.checkandbigdataquery.jpython; import org.python.core.PyFunction; import org.python.core.PyInteger; import org.python.core.PyObject; import org.python.util.PythonInterpreter; public class Fibo { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter = new PythonInterpreter(); interpreter.execfile("./pythonSrc/fibo.py"); PyFunction function = (PyFunction)interpreter.get("fib",PyFunction.class); PyObject o = function.__call__(new PyInteger(8)); System.out.println(o.toString()); } }
package com.xxxx.checkandbigdataquery.jpython; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Cmd { public static void main(String[] args) throws IOException, InterruptedException { String[] arguments = new String[] { "python", "D:\\jython2.7.1\\time.py", "huzhiwei", "25" }; try { Process process = Runtime.getRuntime().exec(arguments); BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); int re = process.waitFor(); System.out.println(re); } catch (Exception e) { e.printStackTrace(); } } }
py代碼
#coding:utf-8 def countNum(param): reslut = "" if(param[1]+param[2]) == 0: reslut ="除數(shù)不能為0" else: res = param[0]/(param[1]+param[2]) reslut ="this count: "+str(res) print(reslut) if __name__=="__main__": countNum([10,2,3])
#!/usr/bin/python #coding=utf-8 #定義一個(gè)方法 def my_test(name, age): print("name: "+str(name)) print(age) #str()防解碼出錯(cuò) return "success" #主程序 #sys.argv[1]獲取cmd輸入的參數(shù) my_test(sys.argv[1], sys.argv[2])
四、解決問(wèn)題
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中g(shù)etattr函數(shù)和hasattr函數(shù)作用詳解
這篇文章主要介紹了Python中g(shù)etattr函數(shù)和hasattr函數(shù)作用的相關(guān)知識(shí),非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06Python之numpy.random.seed()和numpy.random.RandomState()區(qū)別及說(shuō)明
這篇文章主要介紹了Python之numpy.random.seed()和numpy.random.RandomState()區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2017-10-10Python安裝與基本數(shù)據(jù)類(lèi)型教程詳解
這篇文章主要介紹了Python安裝與基本數(shù)據(jù)類(lèi)型教程詳細(xì)講解,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05Python 批量讀取文件中指定字符的實(shí)現(xiàn)
這篇文章主要介紹了Python 批量讀取文件中指定字符的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03使用python實(shí)現(xiàn)3D聚類(lèi)圖示例代碼
這篇文章主要介紹了使用python實(shí)現(xiàn)3D聚類(lèi)圖效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08Python Django框架介紹之模板標(biāo)簽及模板的繼承
今天給大家?guī)?lái)Python Django框架的相關(guān)知識(shí),文中對(duì)模板標(biāo)簽及模板的繼承介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05教你用Python實(shí)現(xiàn)短信驗(yàn)證碼的發(fā)送
當(dāng)我們?cè)谧?cè)一個(gè)網(wǎng)頁(yè)時(shí),有的網(wǎng)頁(yè)會(huì)讓必須要短信驗(yàn)證、郵箱驗(yàn)證,才可以進(jìn)行賬號(hào)的注冊(cè),下面這篇文章主要給大家介紹了關(guān)于用Python實(shí)現(xiàn)短信驗(yàn)證碼發(fā)送的相關(guān)資料,需要的朋友可以參考下2022-12-12