从J调用Python程序

2024-04-26 21:04:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用C Python语言开发的控制台应用程序。我需要从java代码中调用这个程序函数。此外,我还需要在调用main函数后将一些参数(如List<;gt)传递给它。我研究了一个foundthis链接,但它是针对jpython代码的。我们有类似的C Python工具吗?你知道吗


Tags: 工具函数代码ltgt程序语言应用程序
1条回答
网友
1楼 · 发布于 2024-04-26 21:04:00
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SystemCall{

        private static Log logger = LogFactory.getLog(SystemCall.class);

    public static int execute(String command) {
        int result = 0;
        if (command == null){
                throw new RuntimeException("harness command is null");
        }
        Runtime r = Runtime.getRuntime();

        try {
            Process p = r.exec(command);
            try {
                if (p.waitFor() != 0) {
                    result = p.exitValue();
                }
            } catch (InterruptedException e) {
                logger.error("System call interupted.", e);
                //TODO
            } finally {
                //TODO
            }
        } catch (IOException e) {
                logger.error("IO error in system call.", e);
                //TODO
        }
        return result;
     }
 }

相关问题 更多 >