问题描述:

Shouldn't I be able to simply type "python" in my command line and retrieve the text that is spit out?

 try {

Process proc = Runtime.getRuntime().exec("python");

BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));

String result = null;

while ((result = reader.readLine()) != null) {

PrintWriter out = resp.getWriter();

out.println("<div>" + result + "</div>");

}

} catch (IOException e) {

e.printStackTrace();

}

The desired result would be:

Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AM

D64)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>>

网友答案:

Running python C:\Users\Me\HelloWorld.py executes the script and collects the standard output:

try {
        Process proc = Runtime.getRuntime().exec("python C:\Users\Me\HelloWorld.py");
        BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String result = null;
        while ((result = reader.readLine()) != null) {
            PrintWriter out = resp.getWriter();
            out.println("<div>" + result + "</div>");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
相关阅读:
Top