大侠门,请亮剑吧!吗呀!一个奇怪的问题Runtime.getRuntime().exec

huitongrr 2008-07-18 10:53:03
最近做了一个实时监控的程序.
程序主要的中心为
1.----------------------------------------------------------------------
timer=new Timer(true);
timer.schedule(new ICMP() , 0 , Integer.parseInt(str[0][0])*60*1000);//定时执行调用ICMP() 类
2.----------------------------------------------------------------------
ICMP() 类里主要方法为
public synchronized void ping(String host,String nename) {
Process process = Runtime.getRuntime().exec(command);//command为一些ping 的命令.
BufferedReader in = new BufferedReader(new InputStreamReader(
process.getInputStream()));
}
------------------------------------------------------------------------
(注:一般设定为20分钟左右执行一次,而且每次ping的命令都是从固定的列表中取得,都一样,就是每次所执行的ping命令都一样.)
程序现在放在10几台unix服务器上.

但是现在发现,有的机器运行一天左右就会发生

Exception in thread "Timer-0" java.lang.NullPointerException

at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)

这样的错误.
有的机器则是4,5天后才有问题,有的一直没有.每台机器上的程序都一样.

崩溃中!

后来定位错误应该是 发生在这

Process process = Runtime.getRuntime().exec(command);//command为一些ping 的命令.

Runtime.getRuntime()发生的错误!

侠客们,请亮剑!!!砍死我吧!!











...全文
202 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
huitongrr 2008-07-22
  • 打赏
  • 举报
回复
十分感谢 很急啊!!谢谢了~!
huitongrr 2008-07-22
  • 打赏
  • 举报
回复
!!!谁能告诉我
Runtime rt = null;
do{
rt = Runtime.getRuntime();
}while(rt==null);----------------这是不是应该是 while(rt!=null)
Process process = rt.exec(command);
interpb 2008-07-18
  • 打赏
  • 举报
回复
就是执行完之后
huitongrr 2008-07-18
  • 打赏
  • 举报
回复
????
我放在
System.out.println("command:"+command);------------1
Process process = Runtime.getRuntime().exec(command);
process.getErrorStream();//--------放在这?
System.out.println("process:"+process);------------2
BufferedReader in = new BufferedReader(new InputStreamReader(
process.getInputStream()));
huitongrr 2008-07-18
  • 打赏
  • 举报
回复
process.getErrorStream 这句话是做什么用的,我放在哪呢???
huitongrr 2008-07-18
  • 打赏
  • 举报
回复
我都被累脑残了!!呵呵
interpb 2008-07-18
  • 打赏
  • 举报
回复
btw : 楼主这个脑残的图片不错 嘿嘿
interpb 2008-07-18
  • 打赏
  • 举报
回复
process.getErrorStream 看看执行ping 错误输出是什么
huitongrr 2008-07-18
  • 打赏
  • 举报
回复
try {
System.out.println("command:"+command);------------1
Process process = Runtime.getRuntime().exec(command);
System.out.println("process:"+process);------------2
BufferedReader in = new BufferedReader(new InputStreamReader(
process.getInputStream()));




错误发生时,System.out.println("command:"+command);------------1
这条都可以打印出来
但是
System.out.println("process:"+process);------------2
这个就打印不了了!!

所以我觉的是 Process process = Runtime.getRuntime().exec(command); 这出错了!


huitongrr 2008-07-18
  • 打赏
  • 举报
回复
怎么做保护!!!!! 这应该是
timer=new Timer(true);
timer.schedule(new ICMP() , 0 , Integer.parseInt(str[0][0])*60*1000);//定时执行调用ICMP() 类
这的问题吧!!
atealxt 2008-07-18
  • 打赏
  • 举报
回复
试试把

Process process = Runtime.getRuntime().exec(command);//command为一些ping 的命令.

改成:

Runtime rt = null;
do{
rt = Runtime.getRuntime();
}while(rt==null);
Process process = rt.exec(command);//command为一些ping 的命令.

看看行不行?
huitongrr 2008-07-18
  • 打赏
  • 举报
回复

下面是出错的地方.Timer.java:512

private void mainLoop() {
while (true) {
try {
TimerTask task;
boolean taskFired;
synchronized(queue) {
// Wait for queue to become non-empty
while (queue.isEmpty() && newTasksMayBeScheduled)
queue.wait();
if (queue.isEmpty())
break; // Queue is empty and will forever remain; die

// Queue nonempty; look at first evt and do the right thing
long currentTime, executionTime;
task = queue.getMin();
synchronized(task.lock) {
if (task.state == TimerTask.CANCELLED) {
queue.removeMin();
continue; // No action required, poll queue again
}
currentTime = System.currentTimeMillis();
executionTime = task.nextExecutionTime;
if (taskFired = (executionTime<=currentTime)) {
if (task.period == 0) { // Non-repeating, remove
queue.removeMin();
task.state = TimerTask.EXECUTED;
} else { // Repeating task, reschedule
queue.rescheduleMin(
task.period<0 ? currentTime - task.period
: executionTime + task.period);
}
}
}
if (!taskFired) // Task hasn't yet fired; wait
queue.wait(executionTime - currentTime);
}
if (taskFired) // Task fired; run it, holding no locks
task.run();//--------------------------------------------------------------此处为出错地方!!Timer.java:512
} catch(InterruptedException e) {
}
}
}
}
reality 2008-07-18
  • 打赏
  • 举报
回复
Exception in thread "Timer-0" java.lang.NullPointerException
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
这是出错的关键。查看这个地方的代码,做保护。
huitongrr 2008-07-18
  • 打赏
  • 举报
回复
哦!谢谢啊 加分 呵呵
zidasine 2008-07-18
  • 打赏
  • 举报
回复
友情帮顶
当看见回复的时候 点功能下的管理可以看到
huitongrr 2008-07-18
  • 打赏
  • 举报
回复
我的comand不会空的 我把他println出来了
能看见.

1.ping --的命令都一样啊.上几次都没有异常 为什么这次有了呢????ping 会发生什么异常??


2.我后来统一了好几次程序.不会不一样的.(不一样的只是每个程序上ping的机器都不一样.)


3.我的ping是这么做的

for (int i = 0; i < str.length; i++) {
this.ping(str[i][0],str[i][1]);//执行ping 命令
}

ping的方法是 synchronized
public synchronized void ping(String host,String nename) {
}
而且 我发现在正常运行时候,就算上一个ping命令没有ping成功 ,浪费很多时间 ,下一个也不会错误的.


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

huitongrr 2008-07-18
  • 打赏
  • 举报
回复
怎么看不见回复!!!
老紫竹 2008-07-18
  • 打赏
  • 举报
回复
无法定位错误。猜测的原因有

1 你的 ping 发生了异常
2 你不小心更新了程序
3 你的ping 出现了嵌套运行,也就是上一次还没有结束,下一个又开始了


临时建议,将机器的ping的时间分开,不要再某一时刻同时运行,

20分钟间隔,那就每2分钟让1个机器运行,把负载平均一下
M_song 2008-07-18
  • 打赏
  • 举报
回复
最好先通过debug,或者log的方式,看是哪个是空指针!
比如是command,还是Runtime.getRuntime()!
huitongrr 2008-07-18
  • 打赏
  • 举报
回复
哈哈 !!
加载更多回复(7)

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧