質問

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は1文字を読み取り、*文字でマスクされます。

ヘルプ

役に立ちましたか?

解決 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