我正在为在windows命令行上运行的可执行文件创建一个包装器。可执行文件接收一些命令,然后尝试连接到另一个设备。则它输出和ERROR!或准备好"设备名称"在应用程序退出之前,我不会收到此消息。问题是这个应用程序是一个隧道,允许我在外部盒子上运行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
中读取即可获得stdout
和stderr
。
我已经解决了我的问题——我用java执行的应用程序在行的末尾没有EOL——事实上,它们只是挂在行上。例如,telnet等待用户名,然后等待密码。我不确定这是否合适,但它是有效的,也是我现在要使用的
while((i=br.read())!=-1){
ch += (char)i;
}
这会在每个字符输入时输出它们,然后我只需要确保字符串包含我要查找的内容!