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)
为什么启动没有成功呢?正确的启动方法应该是怎样的呢?
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)
为什么启动没有成功呢?正确的启动方法应该是怎样的呢?
...全文
相关推荐
java.lang.Runtime.exec() Payload知识点详解
例如,可以使用以下代码执行命令:`String cmd = "ls -l"; Process process = Runtime.getRuntime().exec(cmd);`避免使用空格的解决方案为了避免使用空格的问题
Java调用shell脚本解决传参和权限问题的方法
().exec(cmd); // ...}
Android下的CMD命令之关机重启及重启recovery
{ try { Process suProcess = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream
Java中如何调用cmd压缩文件
proc = Runtime.getRuntime().exec(cmd);```在这个例子中,`cmd`字符串包含了完整的WinRAR命令行参数。
Java使用Runtime.exec()给Windows命令提示符做了个外壳,真的很山寨!
例如,`Runtime.getRuntime().exec("cmd /c " + command)`会执行一个CMD命令。3.


Java SE

