有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从“可执行”jar调用脚本Python?

我正在尝试创建一个Java应用程序,通过单击按钮可以调用python脚本。在按钮的方法中,我设置了PythonInterpreter和几个变量,这些变量需要作为参数传递给脚本:

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
        PythonInterpreter interp = new PythonInterpreter();

        // Insert arguments as input for the script
        interp.set("path", path);
        interp.set("logcatName", dev1+"_Video"+i+".txt");
        interp.set("logcatSecondDevice", dev2+"_Video"+i+".txt");
        interp.set("descriptionPath", descriptionPath);

        interp.execfile("C:\\adp\\cutVideo.py");

以下是通过传递上述参数来调用脚本中函数的指令:

if __name__ == '__main__':
  cut_video(path, logcatName, logcatSecondDevice, descriptionPath)

如果我使用NetBeans运行这个程序,一切都会完美运行,但是如果我尝试使用我应该创建的jar文件运行,它就不再工作了。 有没有别的办法让它发挥作用


共 (1) 个答案

  1. # 1 楼答案

    我将脚本放在另一个文件夹中,并用指令调用它,从而解决了这个问题:

    ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "start", "python", "C:\\adp\\cutVideo.py", path, descriptionPath);
    Process move = pb.start();