从Java运行Python脚本

从Java运行Python脚本,java,python,digits,pycaffe,Java,Python,Digits,Pycaffe,我试图用Java运行.py脚本,但当我运行Java代码时,它不会显示任何输出。我做错了什么?我试过: ArrayList<String> command = new ArrayList<String>(); //xterm will be launched, if platform is Linux. command.add("xterm"); command.add("-c"); command.add("python"); co

我试图用Java运行.py脚本,但当我运行Java代码时,它不会显示任何输出。我做错了什么?我试过:

ArrayList<String> command = new ArrayList<String>();
    //xterm will be launched, if platform is Linux.
    command.add("xterm");
    command.add("-c");
    command.add("python");
    command.add("/home/clef/Escritorio/use_archive.py");
    command.add("/home/clef/classification/STOP_WORDS.tar.gz");
    command.add("/home/clef/Escritorio/Prueba_linea/000006/6.jpg");
    command.add(" > ~/Escritorio/mike.txt");
    //command.add("--revert");    // switch to revert the patch

    ProcessBuilder pb = new ProcessBuilder(command);

    Process p = null;

    if (pb != null) {

        try {
            p = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (p != null) {
            try {
                p.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line = "";
    ArrayList<String> allOut = new ArrayList<>();
    System.out.println("salida");
    try
    {
        System.out.println("SALIDA-----------:");
        while ((line = reader.readLine())!= null)
        {
            System.out.println(line);
            allOut.add(line);
        }
    } catch (IOException ex)
    {
        //allOut = "0";
        //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        //System.out.println("erro3");        }
        System.out.println("error");
    }
试试这个:

Process p = Runtime.getRuntime().exec("python yourapp.py");

Exec运行系统命令,如果您安装了python,这将像您在操作系统的命令终端中一样运行python文件。

为什么要使用xterm?我也尝试过使用/bin/bash,但效果不好您不需要使用这两种命令,python将自行运行…我想是这样,但脚本不起作用如果我从java运行它,我会尝试像“ls”“cd..”这样的命令,但我不能从java运行python脚本。你说它“不起作用”是什么意思
Process p = Runtime.getRuntime().exec("python yourapp.py");