문제

Java를 사용하여 외부 애플리케이션에 일부 값을 입력하려고합니다.

Java 응용 프로그램은 다음과 같습니다.

Runtime runtime = Runtime.getRuntime();
// ... str build ...
proc = runtime.exec(str);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
bw.write(value);
bw.flush();
bw.close();
if (proc.waitFor() != 0) 
    // error msg
// the end

응용 프로그램은 Waitfor Method에 걸려 있습니다.

외부 응용 프로그램은 다음과 같습니다.

welcome banner

please enter 8 character input:

환영 배너는 printf를 사용하여 인쇄되며 입력은 setConsolemode/readConsoleInput로 가져옵니다. ReadConsoleInput는 하나의 숯을 읽고 * 캐릭터로 가려집니다.

돕다

도움이 되었습니까?

해결책 2

답을 얻었습니다! 트릭은 텍스트가 아닌 키보드 이벤트를 기대하기 때문에 writeconsoleinput () API를 사용하는 것입니다. 그래서 Waitfor ()가 영원히 기다린 이유입니다! :)

다른 팁

당신이 사용할 수있는:

proc.getOutputStream().write("some date".getBytes())

앱이 stdout과 stderr로 보내는 모든 것을 읽어야한다는 점을 명심하십시오. 그렇지 않으면 거기에 갇혀있을 수 있습니다. 일반 클래스를 사용하여 다른 스레드에서 읽습니다. 사용법은 다음과 같습니다.

InputStreamSucker inSucker = new InputStreamSucker(proc.getInputStream());
InputStreamSucker errSucker = new InputStreamSucker(proc.getErrorStream());
proc.waitFor();
int exit = process.exitValue();
inSucker.join();
errSucker.join();

InputStreamSucker 코드는 다음과 같습니다.

public class InputStreamSucker extends Thread
{
    static Logger logger = Logger.getLogger(InputStreamSucker.class);

    private final BufferedInputStream m_in;

    private final ByteArrayOutputStream m_out;

    private final File m_outFile;

    public InputStreamSucker(InputStream in) throws FileNotFoundException
    {
        this(in, null);
    }


    public InputStreamSucker(InputStream in, File outFile) throws FileNotFoundException
    {
        m_in = new BufferedInputStream(in, 4096);
        m_outFile = outFile;
        m_out = new ByteArrayOutputStream();
        start();
    }

    @Override
    public void run()
    {
        try
        {
            int c;
            while ((c = m_in.read()) != -1)
            {
                m_out.write(c);
            }
        }
        catch (IOException e)
        {
            logger.error("Error pumping stream", e);
        }
        finally
        {
            if (m_in != null)
            {
                try
                {
                    m_in.close();
                } 
                catch (IOException e)
                {
                }
            }

            try
            {
                m_out.close();
            }
            catch (IOException e)
            {
                logger.error("Error closing out stream", e);
            }

            if (m_outFile != null)
            {
                byte data[] = m_out.toByteArray();
                if (data.length > 0)
                {
                    FileOutputStream fo = null;
                    try
                    {
                        fo = new FileOutputStream(m_outFile);
                        fo.write(data);
                    }
                    catch (IOException e)
                    {
                        logger.error("Error writing " + m_outFile);
                    }
                    finally
                    {
                        try
                        {
                            if (fo != null) fo.close();
                        }
                        catch (IOException e)
                        {
                            logger.error("Error closing " + m_outFile);
                        }
                    }
                }
            }
        }
    }

    public String getOutput()
    {
        return new String(m_out.toByteArray());
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top