-->

How to run interactive python script using apache

2019-07-20 21:28发布

问题:

I am trying to run a python script using Apache Commons exec.I need to pass some values to python script as python script in an interactive one.How to do it?

My attempt was to set values in parent process's input stream.But it's not working for me.

My code so far:

String line = "python /home/abhijeet/test.py";

    CommandLine cmdLine = CommandLine.parse(line);

    byte buf[]="4".getBytes();

    InputStream io=new ByteArrayInputStream(buf);

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    PumpStreamHandler streamhandler=new PumpStreamHandler(System.out,System.err,io);



    DefaultExecutor executor = new DefaultExecutor();

    executor.setStreamHandler(streamhandler); 

    executor.execute(cmdLine, resultHandler);



    try
    {
        resultHandler.waitFor();
    }

    catch (InterruptedException e) 
    {
        System.out.println("yo errior");
        e.printStackTrace();
    }