簡體   English   中英

進程暫停時Java運行時執行程序getInputStream

[英]Java runtime exec getInputStream when process pauses

我正在為Windows命令行上運行的可執行文件創建包裝器。 該可執行文件需要執行一些命令,然后嘗試連接到另一台設備。 然后輸出並報錯! 或准備好“設備名稱”,直到應用退出,我才收到此消息。 問題是此應用程序是一個隧道,允許我在外部設備上運行telnet,但我需要確保設備已准備就緒,這是我的代碼。

public void startUDPTunnel() {
    //TODO Pull Amino serial number from webportal
    Properties prop = new Properties();
    InputStream inConfig = getClass().getClassLoader().getResourceAsStream("config.properties");
    try {
        prop.load(inConfig);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    String server = prop.getProperty("server");//config.GetProp("server");
    System.out.println(server);
    String port = prop.getProperty("port");//config.GetProp("port");
    System.out.println(port);
    String location = prop.getProperty("location");//config.GetProp("location");
    System.out.println(location);
    String url = prop.getProperty("URL");
    System.out.println(url);
    String input = "";
    try {
        input = getSerial(url);
        System.out.println(input);
        Process p = Runtime.getRuntime().exec(location+"udptunnel.exe -c 127.0.0.1 23 "+input+" "+server+" "+port+" 127.0.0.1 23");  

        threadSleep();
        BufferedReader in = new BufferedReader(
                            new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            if(line.equals("ERROR!")){
                System.out.println("There was an ERROR");
            }
            if(line.equals("Ready for \""+input+"\"")){
                System.out.println("Load Telnet");
            }
        }
        p.destroy();
    } catch (IOException e) {  
        e.printStackTrace();  
    }
}

抱歉,此功能中還有很多調試代碼。

編輯

好的,我可以肯定地知道問題出在哪里bufferReader.readLine()需要\\ n或\\ r或只是掛起,無論如何在沒有緩沖區的情況下觀看流?

您應該使用ProcessBuilder ,然后使用redirectErrorStream() 認為這將導致該進程的stdout無法緩沖。 即使沒有,您也只需要從一個InputStream讀取即可獲取stdoutstderr

我已經解決了我的問題,我用Java執行的應用程序在行尾沒有EOL,實際上它們只是掛在行上,例如telnet等待用戶名然后輸入密碼。 我不確定這是否合適,但它是否有效,這是我現在要使用的

while((i=br.read())!=-1){
    ch += (char)i;
}

當他們剛進入時,這會輸出每個字符,而我只是要確保字符串包含我在尋找什么!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM