您的位置:首页 > 产品设计 > UI/UE

Java使用ProcessBuilder创建子进程出现错误:CreateProcess error=87

2015-03-31 11:36 483 查看
Exception occurred executing command line.
Cannot run program... CreateProcess error=87, The parameter is incorrect


网上说的错误原因是CLASSPATH过长,但我测试发现,还有一种原因

根据ProcessBuilder的构造器,

<pre name="code" class="java">/**
* Constructs a process builder with the specified operating system program and arguments.
* This is a convenience constructor that sets the process builder's command to a string
* list containing the same strings as the command array, in the same order. It is not
* checked whether command corresponds to a valid operating system command.

* Parameters:
* command a string array containing the program and its arguments
**/
public ProcessBuilder(String... command) {
}


对于command参数,首个参数是命令,如果使用的是相对路径,该命令还会从操作系统的PATH环境变量中去搜寻。

如果默认不是.bat或者.exe后缀,比如(.cmd),那么需要显示加上后缀,然后其余是命令后依次所跟的参数。

还有一点需要注意的是:如果ProcessBuilder使用.bat或者.cmd发起的批处理命令,那么返回的Process对象表示的是这个批处理进程,而不是真正所执行的批处理中的那个exe命令子进程,也就是说,当你调用Process对象上的destroy方法,只相当于关闭了命令行窗口,并没有把实质的exe进程关闭掉。

public ProcessBuilder directory(File workingDirectory)
设置当前启动的子进程运行在哪个工作目录下

/**
* Sets the source and destination for subprocess standard I/O to be the same as 
* those of the current Java process. 
* This is a convenience method.
**/
public ProcessBuilder inheritIO();
设置启动的子进程的输入输出控制台与父进程相同

public Process start();
启动子进程,返回Process对象,可以调用该对象的destroy来杀掉子进程

举例:

Process myProc = new ProcessBuilder("mycommand.exe", "arg1", "arg2", "args")
.directory(myWorkingDirectory)
.inheritIO()
.start();
//any other code here
myProc.destroy();



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐