Runtime.getRuntime().exec(cmd)的问题.
请看代码:
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)
为什么启动没有成功呢?正确的启动方法应该是怎样的呢?