代码之家  ›  专栏  ›  技术社区  ›  John

Java在外部C/C++应用中的输入

  •  1
  • John  · 技术社区  · 16 年前

    我试图用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读取一个字符,并用*字符屏蔽。

    帮助

    2 回复  |  直到 16 年前
        1
  •  2
  •   Omry Yadan    16 年前

    你可以使用:

    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();
    

    输入拖缆吸盘代码如下:

    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());
        }
    }
    
        2
  •  0
  •   John    16 年前

    知道答案了!技巧是使用writeconsoleinput()API,因为程序需要键盘事件,而不是文本…这就是为什么waitfor()永远等待的原因!:)