Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中运行此python脚本?_Java_Python - Fatal编程技术网

如何在java中运行此python脚本?

如何在java中运行此python脚本?,java,python,Java,Python,我想在我的java应用程序中使用此脚本: 我环顾四周,试着像这样执行脚本: public static void main(String[] args) throws IOException { String s = null; Process p = Runtime.getRuntime().exec("python C:\\\\Users\\\\Home\\\\work\\\\test.py"); BufferedReader stdIn

我想在我的java应用程序中使用此脚本:

我环顾四周,试着像这样执行脚本:


public static void main(String[] args) throws IOException  {
        String s = null;
        Process p = Runtime.getRuntime().exec("python C:\\\\Users\\\\Home\\\\work\\\\test.py");
         BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));
         while ((s=stdInput.readLine()) != null) {
             System.out.println(s);
         }
    }

但我没有得到任何输出。我需要什么才能运行此特定脚本?

您可以这样使用:

String command = "python /c start python path\to\script\script.py>";
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader stdInput = new BufferedReader(new 
                              InputStreamReader(p.getInputStream()));
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));

    String line;
    while ((line = stdInput.readLine()) != null) {
        System.out.println(line);
      }
      stdInput.close();
      while ((line = stdErr.readLine()) != null) {
        System.out.println(line);
      }
      stdErr.close();
      p.waitFor();
      System.out.println("Done.");

p.destroy();
Protip:始终从“文件资源管理器”选项卡复制路径

既然您得到了JSON响应,请尝试解析它


如果您想使用java大量使用python,请尝试探索

我得到了一个
ModuleNotFoundError
。运行该文件所需的所有文件是否应位于同一位置?