在java程序里调用runting.exec()获得process,然后得到in,out,err三个流,之后从流里读出数据。问题是这样的:要执行的这个外部process是个类似telnet的进程,执行后会进入一个consle,而不是通常的执行一个命令返回一些信息。也就是说此进程会hold住,等待下一次的输入。但是在实际的调试中,从流中只能返回第一次执行该外部进程的信息,也无法再次把命令从程序中传到外部进程去。求解!!
在java程序里调用runting.exec()获得process,然后得到in,out,err三个流,之后从流里读出数据。问题是这样的:要执行的这个外部process是个类似telnet的进程,执行后会进入一个consle,而不是通常的执行一个命令返回一些信息。也就是说此进程会hold住,等待下一次的输入。但是在实际的调试中,从流中只能返回第一次执行该外部进程的信息,也无法再次把命令从程序中传到外部进程去。求解!!
因为这个外部进程主要作用就是返回远程服务器上的信息然后在通过我的程序处理该信息,但是就是无法与其交互,那种只是执行一次然后返回一些信息就推出的进程就没有这个问题,不知道我表到清除了没,,,,,哪位高人帮帮忙?
既然借助了外部程序,怎么还能是平台无关?
“既然借助了外部程序,怎么还能是平台无关”楼上说的是,这就是问题所在,但是java的process是平台无关的,各种平台有各自的此外部进程的实现。通过process来解决平台问题,但是这个process好像功能有限(如上所述)·····
求解
问题1, 你的out中有没有把必要的信息送过去,打个比方:
telnet到一个设备,输入 display,此时,你会输入一个回车确认传送,那么,你的in里面是否有个“回车”(需要确认对端接受的回车的类型\r\n还是\n).问题2, 你的in是否已经全部读完了,这点很重要,对端很有可能是根据你全部度去完成的信息之后开通下一次的交互,从而达到同步的效果
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);
}
}
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;
}
{
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");
}
}