Runtime.getRuntime().exec(cmd)的问题.

tsingien 2004-03-22 05:34:06
请看代码:
import java.lang.*;
import java.io.*;

public class test
{
public static int i;
public Runtime rt;
public Process test1;
public String[] cmd;

public test()
{
Runtime rt = Runtime.getRuntime();
cmd = new String[2];
cmd[0] = "/bin/sh";
cmd[1] = "java /usr/www/html/java/testdir/test1";
}
public int start()
{
i = 0;
try{
test1 = rt.exec(cmd);
}catch(SecurityException e)
{
System.out.println("create process test1 failed1:" + e.getMessage());
return i;
}catch(IOException e)
{
System.out.println("create process test1 failed2:" + e.getMessage());
return i;
}catch(NullPointerException e)
{
System.out.println("create process test1 failed3:" + e.getMessage());
return i;
}catch(IllegalArgumentException e)
{
System.out.println("create process test1 failed4:" + e.getMessage());
return i;
}
i ++;
try{
test2 = rt.exec(cmd);
}catch(Exception e)
{
System.out.println("create process test1 failed:" + e.getMessage());
return i;
}
i ++;
return i;
}
public int stop()
{
i = 0;
test1.destroy();
i ++ ;
test2.destroy();
i ++ ;
return i;
}
public static void main(String []argv)
{
test m = new test();
if(m.start() == 2)
{
System.out.println("启动进程成功");
}
try{
Thread.sleep(3000);
}catch(Exception e){}
if(m.stop() == 2)
{
System.out.println("关闭进程成功");
}
}
}
运行返回:
create process test1 failed3:null
Exception in thread "main" java.lang.NullPointerException
at test.stop(test.java:56)
at test.main(test.java:72)
为什么启动没有成功呢?正确的启动方法应该是怎样的呢?
...全文
296 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
swingsxlong 2004-03-23
  • 打赏
  • 举报
回复
沒有看見Thread,也沒有看見Runnable,為甚麼
System.out.println("启动进程成功");
System.out.println("关闭进程成功");
至於Runtime的exec(String[] )方法我沒用過,不知道用的對不隊,猜不出你的程序究竟有甚麼目的
,直接用exec(String )就好了.
還有,沒有必要catch(SecurityException e),catch(IOException e),catch(NullPointerException e),catch(IllegalArgumentException e),直接用catch(Exception e)就好了,簡潔的多.在你不準備針對每一種異常做出不同的處理的時候,沒必要一個一個catch.
Leemaasn 2004-03-23
  • 打赏
  • 举报
回复
帮你Up一下。。。
nkwj 2004-03-23
  • 打赏
  • 举报
回复
public test()
{
Runtime rt = Runtime.getRuntime();
cmd = new String[2];
cmd[0] = "/bin/sh";
cmd[1] = "java /usr/www/html/java/testdir/test1";
}
===============>
public test()
{
rt = Runtime.getRuntime();
cmd = new String[2];
cmd[0] = "/bin/sh";
cmd[1] = "java /usr/www/html/java/testdir/test1";
}
tsingien 2004-03-23
  • 打赏
  • 举报
回复
怎么有意思?我看论坛上有人这么回答.
知道的给提示一下呗
drinkant 2004-03-22
  • 打赏
  • 举报
回复
cmd = new String[2];
cmd[0] = "/bin/sh";
cmd[1] = "java /usr/www/html/java/testdir/test1";


test1 = rt.exec(cmd);


真是有意思。。。。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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