使用Java PrcoessBuilder运行Python远程文件

时间:2019-01-19 06:46:12

标签: java processbuilder

我正在尝试使用带有以下代码的java Process builder执行python文件(远程)。

 List<String> commands = new ArrayList<String>();
 commands.add("/bin/sh");
 commands.add("-c");
 commands.add("\"python <(curl <remote python file>\"");

 ProcessBuilder builder = new ProcessBuilder(commands);
 builder.redirectErrorStream(true);

及其提供

  

/ bin / sh:python <(curl):没有这样的文件或目录

尝试使用命令插值(curl和python命令前的$不会用双引号引起来)

 commands.add("/bin/sh");
 commands.add("-c");
 commands.add("python <$(curl <remote python file>)");

这是扔

  

模糊的重定向错误

1 个答案:

答案 0 :(得分:0)

Java几乎可以完成curl可以做的所有事情。只需使用代码下载文件即可:

URL remotePythonFile = new URL("http://example.com/remotefile.py");
Path localPythonFile = Files.createTempfile(null, ".py");
try (InputStream remoteStream = remotePythonFile.openStream()) {
    Files.copy(remoteStream, localPythonFile,
        StandardCopyOption.REPLACE_EXISTING);
}

ProcessBuilder builder =
    new ProcessBuilder("python", localPythonFile.toString());