python代码如何转jar包

教程下载地址: 网赚博客http://www.piaodoo.com/创业项目排行榜前十名http://www.piaodoo.com/


一、安装

首先需要安装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])

四、解决问题

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持网赚博客http://www.piaodoo.com/。

                        友情连接:  

茂名一技http://www.szsyby.net/


茂名一技http://www.enechn.com/


美文集http://www.tpyjn.cn/


手游排行前十名http://www.bjkhrx.com/

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值