在java程序里调用runting.exec()获得process,然后得到in,out,err三个流,之后从流里读出数据。问题是这样的:要执行的这个外部process是个类似telnet的进程,执行后会进入一个consle,而不是通常的执行一个命令返回一些信息。也就是说此进程会hold住,等待下一次的输入。但是在实际的调试中,从流中只能返回第一次执行该外部进程的信息,也无法再次把命令从程序中传到外部进程去。求解!!

解决方案 »

  1.   

    你能用in流往里读,就能用out流往外写不就完了嘛
      

  2.   

    给我的感觉是in里只能返回本地的输出信息,经过网络返回的信息不知什么原因无法从in里得到
    因为这个外部进程主要作用就是返回远程服务器上的信息然后在通过我的程序处理该信息,但是就是无法与其交互,那种只是执行一次然后返回一些信息就推出的进程就没有这个问题,不知道我表到清除了没,,,,,哪位高人帮帮忙?
      

  3.   

    或者哪位达人,做过java与jvm外的进程同步数据程序的兄弟给点意见
      

  4.   

    另外:大家不要推荐jni,俺做的是平台无关的,thanks
      

  5.   

    不是任何程序的输出都可以捕获的,为什么不用java自己的类和远程通信,非要借助额外软件。
    既然借助了外部程序,怎么还能是平台无关?
      

  6.   

    不能用java类和远程通信,因为没有提供接口,无法从api级别调用
    “既然借助了外部程序,怎么还能是平台无关”楼上说的是,这就是问题所在,但是java的process是平台无关的,各种平台有各自的此外部进程的实现。通过process来解决平台问题,但是这个process好像功能有限(如上所述)·····
    求解
      

  7.   

    不能用Runtime.exec而应该用ProcessBuilder.start()。这样会把进程的控制台去除,把三个流归到JAVA程序中来,方便操作
      

  8.   

    你的in会的对端需要你送东西过去的他才会返回问题:只能返回第一次执行该外部进程的信息,也无法再次把命令从程序中传到外部进程去。看看你的问题是否是因为,
    问题1, 你的out中有没有把必要的信息送过去,打个比方:
    telnet到一个设备,输入 display,此时,你会输入一个回车确认传送,那么,你的in里面是否有个“回车”(需要确认对端接受的回车的类型\r\n还是\n).问题2, 你的in是否已经全部读完了,这点很重要,对端很有可能是根据你全部度去完成的信息之后开通下一次的交互,从而达到同步的效果
      

  9.   

    to:楼上 exec()的时候会返回一些链接信息,所以这个不是问题我是调用的in.ready()来判断是否可读的
      

  10.   

    你的这种方案不太好,从java程序启动一个外部进程而又要控制它,显然,问题变成外部进程成为Java程序的“internal”,这与外部进程是矛盾的。
      

  11.   

    import java.io.*;
    import java.util.*;public class TestExec ...{   
        public void runbat(int timeFortmat) ...{
            String cmd = "cmd /c start D:/ScheduleRun/data/"+timeFortmat+".bat";        try ...{
                Process ps = Runtime.getRuntime().exec(cmd);
                System.out.println(ps.getInputStream());
            } catch(IOException ioe) ...{
                ioe.printStackTrace();
            }
        }          
        
        public static void main(String[] args)...{
               TestExec  test1 = new TestExec ();           
               test1.runbat(1340);
        }
    }
      

  12.   

    public boolean monitor(String hostaddr){
        boolean result = false;
        byte[] rt = new byte[1000];
        try {
          Runtime r = Runtime.getRuntime();
          String cmd = monitorCmd + " " + hostaddr + " " + monitorCmdPara;
          Process p = r.exec(cmd);
          java.io.InputStream is = p.getInputStream();
          int i = 0;
          while (true) {
            rt[i] = (byte) is.read();
            if (rt[i] == -1) { rt[i]='\0'; break;}
            i++;
          }
          is.close();
        }catch (Exception e) {
          e.printStackTrace();
        }
        String trail = new String(rt).trim();
        if (trail.indexOf(responseKey) > 0) {
          result = true;
        }
        return result;
      }
      

  13.   

    我把代码贴出来,大家指点以下是初始化和获得io,和主逻辑无关的代码比较多,我把重点的行加*号了 public void execute() throws AgentException
      {
        try
        {
          verify();
          if (environmentArray == null || environmentArray.length <= 0)
          {
            log.logp(Level.FINE, "ExternalProcess", "execute",
                (new StringBuilder()).append("Executing: ")
                    .append(formattedCommand).toString());
    **      process = Runtime.getRuntime().exec(commandArray);
          }
          else
          {
            log.logp(Level.FINE, "ExternalProcess", "execute",
                (new StringBuilder()).append("Executing: ")
                    .append(formattedCommand).toString());
    **      process = Runtime.getRuntime().exec(commandArray, environmentArray);
          }
          lastConfirmedTime = System.currentTimeMillis();
    **      errorReader = new BufferedReader(new InputStreamReader(process
              .getErrorStream()));
    **      inputReader = new BufferedReader(new InputStreamReader(process
              .getInputStream()));
    **      outputWriter = new BufferedWriter(new OutputStreamWriter(process
              .getOutputStream()));
          processSentinel = new ProcessSentinel();
          processSentinel.start();
          processKeeper = new ProcessKeeper();
          processKeeper.start();
          processErrorConsumer = new ProcessErrorConsumer();
          processErrorConsumer.start();
        }
        catch (IOException e)
        {
          destroy();
          throw AgentException.createAgentException(2011, e, log, getClass()
              .getName(), "execute");
        }
      }以下是重点:  public synchronized String getNextLine() throws AgentException
      {
        if (inputReader == null)
          return null;
        String line;    try
        {
          do
          {
            if (inputReader.ready())
            {
              line = inputReader.readLine();
              if (line == null)
              {
                inputReader = null;
                setExitCode(process.waitFor());
              }
              else
              {
                log.logp(Level.FINEST, "ExternalProcess", logCommand, line);
              }
              setLastConfirmedTime();
              return line;
            }
            else
            {
              wait(1000L);
            }
          } while (inputReader.ready() || getExitCode() == null);
          inputReader = null;
          return null;
        }
        catch (IOException e)
        {
          destroy();
          throw AgentException.createAgentException(2011, e, log, getClass()
              .getName(), "getNextLine");
        }
        catch (InterruptedException e)
        {
          destroy();
          throw AgentException.createAgentException(2013, e, log, getClass()
              .getName(), "getNextLine");
        }
      }