2012-01-15 44 views
1

我试图在我的计算机上la一些蝙蝠文件, 我正在使用Process.exec来执行它。 问题是bat文件启动另一个进程,并且我的程序正在等待它完成,但我需要我的程序停止,尽管子进程仍在运行。 下面是我想要做的一个例子,在这段代码中,我正在开发记事本,程序一直卡住,直到我手动关闭notepd窗口 - 所以我应该为我的程序做些什么来结束记事本会继续运行吗?Java - 如何启动和忘记进程

public static Process test3() throws IOException 
    { 
    Process p; 
    try 
     { 
     p = Runtime.getRuntime().exec("notepad"); 
     } 
    catch (IOException e) 
     { 
     return null; 
     } 
     try 
     { 
     p.waitFor(); 
     } 
     catch (InterruptedException e) 
     { 
     Thread.currentThread().interrupt(); 
     } 


    p.getInputStream().close(); 
    p.getOutputStream().close(); 
    p.getErrorStream().close(); 

    return p; 
    } 
+0

http://stackoverflow.com/questions/931536/how-do-i-launch-a-completely-independent-process-from-a-java-program – Mikhail 2012-01-15 06:59:24

回答

0

如果你在Windows中运行,试试这个:

Runtime.getRuntime().exec("cmd /c start notepad"); 
+3

向下投票,下次发表评论! (那是什么错误?) – aviad 2012-01-15 08:16:34

+0

@aviad:太真实了,我们必须总是说出理由。问候 – 2012-01-15 08:34:09

0

可能,这将帮助:

public class ExternalProcess 
{ 
    public static void main(String... args) throws Exception 
    { 
     Process p = Runtime.getRuntime().exec("Notepad"); 
     p.getInputStream().close(); 
     p.getOutputStream().close(); 
     p.getErrorStream().close(); 
     System.exit(0); 
    } 
} 

只需使用System.exit(0);

Regards

0

我建议你看看ProcessBuilder类以获得更大的灵活性!