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


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();

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值