Question

I'm trying to enter some value in external application using Java.

Java application looks like this:

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

Application hangs at waitFor method.

External application looks like this:

welcome banner

please enter 8 character input:

Welcome banner is printed using printf and input is taken with SetConsoleMode/ReadConsoleInput. ReadConsoleInput reads one char and they are masked with * character.

Help

Was it helpful?

Solution 2

Got the answer! The trick is to use WriteConsoleInput() API because program expects keyboard event, not text ... That's why the waitFor() waited forever! :)

OTHER TIPS

you can use:

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

keep in mind that you HAVE to read everything the app send to stdout and stderr, else it might get stuck writing there. I use a generic class to read it in a different thread. usage is like:

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

InputStreamSucker code is here:

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());
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top