python代碼如何轉(zhuǎn)jar包
更新時間:2024年03月28日 08:58:38 作者:灬點點
這篇文章主要介紹了python代碼如何轉(zhuǎn)jar包問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
一、安裝
首先需要安裝jython-installer-2.7.1的jar包工具。

二、注意需要默認安裝

三、使用cmd進入jython環(huán)境
3.1、配置環(huán)境變量

3.2、注冊環(huán)境點擊運行jython_regrtest.bat

3.3、最后后使用命令進行轉(zhuǎn)換,但是只有jython-installer-2.2.0版本才能使用命令進行轉(zhuǎn)換,由于jython-installer-2.7.0的版本升級,現(xiàn)在直接類型配置文件或者寫在run()或者main()方法中才能運行。
引入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();
}
/**
* 運行python文件
*/
public static void test1() {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:/jython2.7.1/test.py");
}
/**
* 運行python 代碼
*/
public static void test2(){
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("print('hello')");
}
/**
* 手動添加第三方庫路徑
* 把第三方庫文件夾放到執(zhí)行的.py腳本同級目錄
*/
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
#定義一個方法
def my_test(name, age):
print("name: "+str(name))
print(age) #str()防解碼出錯
return "success"
#主程序
#sys.argv[1]獲取cmd輸入的參數(shù)
my_test(sys.argv[1], sys.argv[2])
四、解決問題

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用smtplib庫開發(fā)一個郵件自動發(fā)送工具
在現(xiàn)代軟件開發(fā)中,自動化郵件發(fā)送是一個非常實用的功能,無論是系統(tǒng)通知、營銷郵件、還是日常工作報告,Python的smtplib庫都能幫助我們輕松實現(xiàn)這些功能,本教程將詳細介紹如何使用Python的smtplib庫開發(fā)一個功能完整的郵件自動發(fā)送工具,需要的朋友可以參考下2025-06-06

