Spring boot integrated python engine quick start demo

HBLOG
2 min readMar 12, 2024

--

一、Python introduction

Python is a high-level scripting language that combines interpreted, compiled, interactive and object-oriented scripting. Python is designed to be highly readable. Compared with other languages, it often uses English keywords and some punctuation marks in other languages. It has a more distinctive grammatical structure than other languages.

二、Code Project

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>python</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-python -->
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

application.yaml

server:
port: 8088

DemoApplication.java

package com.et.python;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

Code repository

1. Directly execute python statements in the java class

package com.et.python.util;
import org.python.util.PythonInterpreter;
public class JavaRunPython {

public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("a='hello world'; ");
interpreter.exec("print a;");
}
}

result

hello world

2. Call the python script directly in java

create a python script in resource/py directory,file named test.py,file contents as follows:

a = 1
b = 2
print (a + b)

JavaPythonFile.java

package com.et.python.util;
import org.python.util.PythonInterpreter;
public class JavaPythonFile {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:\\IdeaProjects\\ETFramework\\python\\src\\main\\resources\\py\\test.py");
}
}

result

3

3.use Runtime.getRuntime() to execute python script(recommend)

Create a python script in the resource/py folder. The file name is runtime.py. The file content is as follows:

print('RuntimeDemo')

RuntimeFunction.java

etc: Some codes may trigger sensitive words, so they can only be replaced with pictures.

execute

RuntimeDemo

4. call the function in the python script

create a python script in the resource/py folder,file name add.py,the file content as follows:

def add(a,b):
return a + b

Function.java

package com.et.python.util;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class Function {

public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:\\IdeaProjects\\ETFramework\\python\\src\\main\\resources\\py\\add.py");
PyFunction pyFunction = interpreter.get("add", PyFunction.class);
int a = 5, b = 10;
PyObject pyobj = pyFunction.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("the anwser is: " + pyobj);
}
}

result

the anwser is: 15

四、reference

--

--

HBLOG
HBLOG

Written by HBLOG

talk is cheap ,show me your code

No responses yet