当前位置: 代码网 > it编程>前端脚本>Python > python代码如何转jar包

python代码如何转jar包

2024年05月18日 Python 我要评论
一、安装首先需要安装jython-installer-2.7.1的jar包工具。下载地址:https://www.jython.org/二、注意需要默认安装三、使用cmd进入jython环境3.1、配

一、安装

首先需要安装jython-installer-2.7.1的jar包工具。

下载地址:https://www.jython.org/

二、注意需要默认安装

三、使用cmd进入jython环境

3.1、配置环境变量

3.2、注册环境点击运行jython_regrtest.bat

3.3、最后后使用命令进行转换,但是只有jython-installer-2.2.0版本才能使用命令进行转换,由于jython-installer-2.7.0的版本升级,现在直接类型配置文件或者写在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')");
    }

    /**
     * 手动添加第三方库路径
     * 把第三方库文件夹放到执行的.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 ="除数不能为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输入的参数
my_test(sys.argv[1], sys.argv[2])

四、解决问题

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com