1

我正在尝试使用 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 方法处挂起。

外部应用程序如下所示:

welcome banner

please enter 8 character input:

欢迎横幅使用 printf 打印,输入使用 SetConsoleMode/ReadConsoleInput。ReadConsoleInput 读取一个字符,它们被 * 字符掩盖。

帮助

4

2 回答 2

2

您可以使用:

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());
    }
}
于 2009-09-14T12:38:43.930 回答
0

得到了答案!诀窍是使用 WriteConsoleInput() API,因为程序需要键盘事件,而不是文本......这就是 waitFor() 永远等待的原因!:)

于 2009-09-18T20:22:52.993 回答