我正在尝试使用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读取一个char,它们用*字符掩盖。

帮助

有帮助吗?

解决方案 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