IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    在Java执行Python脚本3种方法

    SparkAndShine发表于 2021-02-18 11:33:51
    love 0

    本文介绍在Java执行Python脚本3种方法,使用Java实现的Python解析器Jython,使用Runtime运行Shell命令,使用ProcessBuilder创建操作系统进程。

    1. 使用Jython

    Jython是一个Java实现的Python解释器。org.python.util.PythonInterpreter类是Jython解释器的标准封装。

    The PythonInterpreter class is a standard wrapper for a Jython interpreter for embedding in a Java application.

    一个简单实例

    HelloWorld.java如下:

    import java.io.*;
    import org.python.core.PyInstance;  
    import org.python.util.PythonInterpreter;  
    
    public class HelloWorld {  
        public static void main(String[] args) {  
            System.out.println("Hello Java World!");  
    
            PythonInterpreter interpreter = new PythonInterpreter();  
            InputStream pyfile = new FileInputStream("Hello.py"); 
            interpreter.execfile(pyfile);  ///执行python py文件
            filepy.close();
        }  
    }

    Hello.py如下:

    #!/usr/bin/env python
    
    print("Hello Python!")

    Hello.sh如下:

    javac HelloWorld.java
    java HelloWorld

    2. 使用Runtime

    使用java.lang.Runtime.exec,执行Shell的命令。

    public Process exec(String command) throws IOException
    public Process exec(String command, String[] envp) throws IOException
    public Process exec(String command, String[] envp, File dir) throws IOException

    一个简单实例

    import java.io.*;
    
    public class HelloWorld_Runtime {  
        public static void main(String[] args) {  
            System.out.println("Hello Java World!");  
    
            // get the current runtime assosiated with this process
            Runtime run = Runtime.getRuntime();
    
            // Executes the specified string command in a separate process with the specified environment and working directory.
            Process process = run.exec("python Hello.py");
        }  
    } 

    执行上述代码,提示错误:

    HelloWorld_Runtime.java:12: error: unreported exception IOException; must be caught or declared to be thrown
            Process process = run.exec("python Hello.py");
                                      ^

    3. 使用ProcessBuilder

    使用ProcessBuilder创建操作系统进程。

    public ProcessBuilder command(String... command)  // java.lang.ProcessBuilder.command()
    
    Process p = new ProcessBuilder("myCommand", "myArg").start();

    一个简单实例

    import java.io.*;
    
    public class HelloWorld_ProcessBuilder {  
        public static void main(String[] args) {  
            System.out.println("Hello Java World!");  
    
            // get the current runtime assosiated with this process
            Runtime run = Runtime.getRuntime();
    
            // Create operating system processes, set this process builder's operating system program and arguments, start a new process
            Process p = new ProcessBuilder().command("python Hello.py").start();
        }  
    } 

    执行上述代码,提示错误:

    HelloWorld_ProcessBuilder.java:12: error: unreported exception IOException; must be caught or declared to be thrown
            Process p = new ProcessBuilder().command("python Hello.py").start();
                                                                             ^



沪ICP备19023445号-2号
友情链接