简体   繁体   中英

Java Process InputStream in Linux machine

I'm using Java Process as below :

Runtime rt = Runtime.getRuntime();
    try {
        Process pr = rt.exec(cmd);

        response = IOUtils.toString(pr.getInputStream(), "UTF-8");
        response = response.substring(2, response.length()-4);
        logger.debug("response :" + response);
    } catch (IOException e) {
        logger.error(e);
        e.printStackTrace();
    }

I can see the loggers in my local machine(Mac OSX). But when I run the same program in a Redhat Linux machine, I'm having problem seeing the logs. I don't have any clue whats happening after pr.getInputStream() . Having a logger before that statement is working fine. I want to see the response of the exec(cmd) . How can I achieve this. I suspect something is wrong when I'm trying to read the InputStream.

Try reading input stream and error stream in two threads other than the one that launched the process. I have seen processes gets stuck writing to error or output streams if no one is reading what they are writing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM