我正试图在我的计算机上laumch一些bat文件,我正在使用Process.exec来执行此操作。问题是bat文件启动了另一个进程,我的程序正在等待它完成,但我需要我的程序停止,尽管子进程仍在运行。下面是我要做的一个例子,在这段代码中,我启动记事本,程序一直被卡住,直到我手动关闭notepd窗口——那么我应该做什么才能让我的程序结束,记事本继续运行?
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;
}
如果您在Windows中运行,请尝试以下操作:
Runtime.getRuntime().exec( "cmd /c start notepad" );
这可能会有所帮助:
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);
问候
我建议您查看ProcessBuilder类以获得更大的灵活性!!!