双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

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

tsingien tsingien 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请看代码:
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)
为什么启动没有成功呢?正确的启动方法应该是怎样的呢?
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
tsingien
Java SE 62,621 成员 307,251 内容