Process p = null;try
{
    p = r.exec("mstsc.exe");
}
catch (IOException e)
{
e.printStackTrace();
} finally {
     try
    {
       Thread.sleep(5000);
     }
    catch (InterruptedException e)
   {
    e.printStackTrace();
    }
    if (p != null) {
         p.destroy();
    }
}

解决方案 »

  1.   

    谢谢
    必须先声明 指向null 吗?直接用不行吗?
      

  2.   

    Process p = r.exec("mstsc.exe"); //这里p是局部变量
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }p1.destroy();//p1哪里来的?改为p,但是改为p,获取不到p,因为它是局部变量。你要声明一个全局变量。