How to call Python in Java
#The Python language has a wealth of system management, data processing, and statistical software packages, so the need to call Python code from Java applications is very common and practical. DataX is an offline synchronization tool for heterogeneous data sources open sourced by Alibaba. It is dedicated to achieving stable and efficient data between various heterogeneous data sources including relational databases (MySQL, Oracle, etc.), HDFS, Hive, ODPS, HBase, FTP, etc. Sync function. Datax also calls Python scripts through Java.
Java core
Java provides two methods, namely ProcessBuilder API and JSR-223 Scripting Engine.
Use ProcessBuilder
Create a local operating system process through ProcessBuilder to start python and execute the Python script. The hello.py script simply outputs "Hello Python!". The development environment needs to have python installed and environment variables set.
@Test public void givenPythonScript_whenPythonProcessInvoked_thenSuccess() throws Exception { ProcessBuilder processBuilder = new ProcessBuilder("python", resolvePythonScriptPath("hello.py")); processBuilder.redirectErrorStream(true); Process process = processBuilder.start(); List<String> results = readProcessOutput(process.getInputStream()); assertThat("Results should not be empty", results, is(not(empty()))); assertThat("Results should contain output of script: ", results, hasItem(containsString("Hello Python!"))); int exitCode = process.waitFor(); assertEquals("No errors should be detected", 0, exitCode); } private List<String> readProcessOutput(InputStream inputStream) throws IOException { try (BufferedReader output = new BufferedReader(new InputStreamReader(inputStream))) { return output.lines() .collect(Collectors.toList()); } } private String resolvePythonScriptPath(String filename) { File file = new File("src/test/resources/" + filename); return file.getAbsolutePath(); }
To rewrite this sentence, you can say: Start using the Python command with one argument, which is the full path to the Python script. It can be placed in the resources directory of the java project. What needs to be noted is: redirectErrorStream(true), in order to make the error output stream be merged into the standard output stream when an error occurs when executing the script. Error information can be read by calling the getInputStream() method of the Process object. Without this setting, you need to use two methods to obtain the stream: getInputStream() and getErrorStream(). After getting the Process object from ProcessBuilder, verify the results by reading the output stream.
Using Java Script Engine
The JSR-223 specification was first introduced in Java 6. The specification defines a set of scripting APIs that can provide basic scripting functionality. These APIs provide mechanisms for sharing values and executing scripts between Java and scripting languages. The main purpose of this specification is to unify the interaction between Java and dynamic scripting languages that implement different JVMs. Jython is a Java implementation of Python that runs on the JVM. Assuming we have Jython on the CLASSPATH, the framework automatically discovers that we have the possibility to use that scripting engine and allows us to request the Python scripting engine directly. In Maven, we can reference Jython, or download and install it directly
<dependency> <groupId>org.python</groupId> <artifactId>jython</artifactId> <version>2.7.2</version> </dependency>
You can list all supported script engines through the following code:
public static void listEngines() { ScriptEngineManager manager = new ScriptEngineManager(); List<ScriptEngineFactory> engines = manager.getEngineFactories(); for (ScriptEngineFactory engine : engines) { LOGGER.info("Engine name: {}", engine.getEngineName()); LOGGER.info("Version: {}", engine.getEngineVersion()); LOGGER.info("Language: {}", engine.getLanguageName()); LOGGER.info("Short Names:"); for (String names : engine.getNames()) { LOGGER.info(names); } } }
If Jython is available in the environment, you should see To the corresponding output:
...
Engine name: jython
Version: 2.7.2
Language: python
Short Names:
python
jython
Now use Jython to call the hello.py script:
@Test public void givenPythonScriptEngineIsAvailable_whenScriptInvoked_thenOutputDisplayed() throws Exception { StringWriter writer = new StringWriter(); ScriptContext context = new SimpleScriptContext(); context.setWriter(writer); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("python"); engine.eval(new FileReader(resolvePythonScriptPath("hello.py")), context); assertEquals("Should contain script output: ", "Hello Python!", writer.toString().trim()); }
Using this API is more concise than the above example. When setting the ScriptContext, you need to include a StringWriter in order to save the output of the script execution. Then provide a short name for ScriptEngineManager to find the script engine, which can use python or jython. Finally, verify that the output is consistent with expectations.
In fact, you can also use the PythonInterpretor class to directly call the embedded python code:
@Test public void givenPythonInterpreter_whenPrintExecuted_thenOutputDisplayed() { try (PythonInterpreter pyInterp = new PythonInterpreter()) { StringWriter output = new StringWriter(); pyInterp.setOut(output); pyInterp.exec("print('Hello Python!')"); assertEquals("Should contain script output: ", "Hello Python!", output.toString().trim()); } }
The exec method provided by the PythonInterpreter class can directly execute Python code. Capture the execution output via a StringWriter as in the previous example. Let’s look at another example:
@Test public void givenPythonInterpreter_whenNumbersAdded_thenOutputDisplayed() { try (PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("x = 10+10"); PyObject x = pyInterp.get("x"); assertEquals("x: ", 20, x.asInt()); } }
The above example can use the get method to access the variable value. The following example shows how to catch errors:
try (PythonInterpreter pyInterp = new PythonInterpreter()) { pyInterp.exec("import syds"); }
Running the above code will throw a PyException exception, which is the same as executing the Python script locally to output an error.
Here are a few things to note:
PythonIntepreter implements AutoCloseable and is best used with try-with-resources.
The PythonIntepreter class name is not a parser representing Python code. Python programs run in Jython in jvm and need to be compiled into java bytecode before execution.
Although Jython is a Python implementation of Java, it may not contain all the same sub-packages as native Python.
The following example shows how to assign java variables to Python variables:
import org.python.util.PythonInterpreter; import org.python.core.*; class test3{ public static void main(String a[]){ int number1 = 10; int number2 = 32; try (PythonInterpreter pyInterp = new PythonInterpreter()) { python.set("number1", new PyInteger(number1)); python.set("number2", new PyInteger(number2)); python.exec("number3 = number1+number2"); PyObject number3 = python.get("number3"); System.out.println("val : "+number3.toString()); } } }
The above is the detailed content of How to call Python in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.
