Preface
In modern development, combining the advantages of different programming languages can often achieve twice the result with half the effort. As a strongly typed, object-oriented programming language, Java has unique advantages in performance and maintainability; while Python is known for its concise syntax and rich library ecosystem. In some projects, we may need to call Python scripts or functions in Java code to take advantage of Python's features to handle specific tasks. This article will introduce in detail four methods to call Python in Java, and recommend one of the most commonly used and practical methods.
1. Directly execute Python statements in Java classes
Through the Jython library, Python statements can be executed directly in Java classes. Jython is a Java implementation of Python that allows seamless embedding of Python code into Java programs. With Jython, you can write Python statements in Java code, and even call Python modules and functions.
step:
- download
- exist
- Using Jython's
PythonInterpreter
kind
Sample code:
import ; public class JythonExample { public static void main(String[] args) { PythonInterpreter interpreter PythonInterpreter Python PythonInterpreter interpreter = new PythonInterpreter(); ( interpreter "print('Hello from Python')"); } } } }
Notes:
- Jython only supports Python 2.7. If you need to support Python, you need to consider other methods.
2. Directly call Python scripts in Java
Another way to directly call Python scripts is to use JavaProcessBuilder
kind. This method allows us to execute external Python scripts in a Java program and get their output results.
step:
- Make sure that the Python interpreter is installed on the system.
- use
ProcessBuilder
kind
Sample code:
import ; import ; public class ProcessBuilderExample { public static void main(String[] args) { try { ProcessBuilder pb ProcessBuilder Process ProcessBuilder pb = new ProcessBuilder("python", ""); Process process Process Process process = (); BufferedReader in BufferedReader BufferedReader in = new BufferedReader(new InputStreamReader(())); String line; String line; String line; String line String while ((line = ()) != null) { (line); } } (line); } } (line); } (line); (line); (line System catch (Exception e) { (); } } } (); } } (); }
Notes:
- Python
3. ★★★ Use () to execute Python script files (recommended)
Common ways to call Python scripts in Java are through()
Methods execute external processes. Compared withProcessBuilder
,Runtime
The method is simpler and suitable for use in simple scenarios.
step:
- use
()
Methods execute Python script files. - pass
getInputStream()
Get the output result of a Python script.
Sample code:
import ; import ; public class RuntimeExample { public static void main(String[] args) { try { Process process Process Process process = ().exec("python "); BufferedReader in = new BufferedReader(new InputStreamReader(())); String line; while ((line = ()) != null) { (line); } } (line); } } (line); } (line); } (line); (line); (line System catch (Exception e) { (); } } }
Reasons for recommendation:
-
()
The method is simple and easy to use, suitable for use in scenarios where Python scripts need to be executed quickly.
4. Call functions in Python scripts
In some complex scenarios, it may be necessary to call specific functions in Python scripts in Java. This can be achieved by executing Python scripts, passing parameters or using interactive methods to obtain the function execution results.
step:
- use
ProcessBuilder
or()
Execute Python scripts. - Define functions in Python scripts and pass parameters in Java code.
- Get the return result of function execution and process it in Java code.
Sample code:
import ; import ; public class CallPythonFunctionExample { public static void main(String[] args) { try { String[] command = {"python", "", "arg1", "arg2"}; Process process = ().exec(command); BufferedReader in = new BufferedReader(new InputStreamReader(())); String line; while ((line = ()) != null) { (line); } } catch (Exception e) { (); } } }
Notes:
- Ensure that the Python script correctly handles the passed parameters and outputs results that can be read by Java programs.
Summarize
There are many implementation methods for calling Python code in Java, from directly executing Python statements to running a complete Python script, each method has its own applicable scenarios. Depending on actual project needs, choosing the right plan will help improve development efficiency. Among them, it is recommended to use()
method because it is simpler and easier to implement.
This is the end of this article about the four methods of calling Python using Java. For more information about the content of the four methods of calling Python in Java, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!