SoFunction
Updated on 2024-11-21

How to convert python code to jar package

I. Installation

First you need to install the jython-installer-2.7.1 jar package tool.

Download Address:/

II. Note the need for a default installation

Third, use cmd to enter the jython environment

3.1 Configuring environment variables

3.2. Register the environment and click Runjython_regrtest.bat

3.3. After the final use of the command to convert, but only thejython-installer-2.2.0version in order to use the command to convert, due to thejython-installer-2.7.0version upgrade, it is now straightforward to type configuration files or write them in therun() or main() method in order to run.

pull into

        <dependency>
            <groupId></groupId>
            <artifactId>jython-standalone</artifactId>
            <version>2.7.0</version>
        </dependency>

java code

package ;

import ;
import ;
import ;

public class HelloPython {

    public static void main(String[] args) {
         test1();
         //test2();
         //test3();
    }

    /**
     * Run the python file
     */
    public static void test1() {
        PythonInterpreter interpreter = new PythonInterpreter();
        ("D:/jython2.7.1/");
    }

    /**
     * Run the python code
     */
    public static void test2(){
        PythonInterpreter interpreter = new PythonInterpreter();
        ("print('hello')");
    }

    /**
     * Manually add the third-party library path
     * Place the third-party libraries folder in the same level directory as the executed .py scripts
     */
    public static  void test3(){
        PySystemState sys = ();
        (());
        ("F:\\Python27\\Lib\\site-packages\\jieba");
    }
}

package ;

import ;
import ;
import ;
import ;

public class Fibo {

    public static void main(String[] args) {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter = new PythonInterpreter();
        ("./pythonSrc/");
        PyFunction function = (PyFunction)("fib",);
        PyObject o = function.__call__(new PyInteger(8));
        (());
    }
}

package ;

import ;
import ;
import ;

public class Cmd {

    public static void main(String[] args) throws IOException, InterruptedException {
        String[] arguments = new String[] { "python", "D:\\jython2.7.1\\", "huzhiwei", "25" };
        try {
            Process process = ().exec(arguments);
            BufferedReader in = new BufferedReader(new InputStreamReader(()));
            String line;
            while ((line = ()) != null) {
                (line);
            }
            ();
            int re = ();
            (re);
        } catch (Exception e) {
            ();
        }
    }

}

py code

#coding:utf-8
def countNum(param):
    reslut = ""
    if(param[1]+param[2]) == 0:
        reslut ="The divisor cannot be zero."
    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

#Define a method
def my_test(name, age):
    print("name: "+str(name))
    print(age)  #str() Anti decoding error
    return "success"
# Main program
#[1] Get parameters for cmd inputs
my_test([1], [2])

IV. Problem solving

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.