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();
相关文章推荐
- 使用Cisco Network assistant时出现“Could not create Java Virtual machine”错误
- 使用Navicat V8.0创建数据库,外键出现错误ERROR 1005: Can't create table (errno: 121)
- 使用HBAdmin创建一个对象时出现java.lang.NoClassDefFoundError的错误
- 使用Navicat V8.0创建数据库,外键出现错误ERROR 1005: Can't create table (errno: 121)
- IIS出现“另一个程序正在使用此文件,进程无法访问。”错误提示的解决办法
- C#使用File.Create创建文件后,报错另一进程正在使用该文件
- 一种在注入进程中使用WTL创建无焦点不在任务栏出现“吸附”窗口的方法和思路
- 打开eclipse时出现 failed to create the Java Virtual Machine的错误
- BUG:Visual Studio 6.0 中出现错误消息“Setup Was Unable to Create a DCOM User Account”(安装程序无法创建 DCOM 用户帐户)
- 如何处理Jdeveloper 启动时出现的错误-Unable to create an instance of the Java Virtual Machine
- 关于C++调用jar包,在创建虚拟机时使用JNI_CreateJavaVM调用失败返回-1的问题
- 启动IIS 网站出现错误 另一个程序正在使用此文件,进程无法访问
- 【原创】Eclipse启动出现“Failed to create the Java Virtual Machine”错误
- java开源框架集成常见错误之使用Criteria添加查询条件出现java.lang.ClassCastException异常
- 安装Xen、创建Xen虚拟机、使用Xen虚拟机过程中经常出现的错误总结
- 使用VS2003创建WEB程序的时候出现"AutoMation服务器不能创建对象"错误
- StreamWriter出现下面错误“正由另一进程使用,因此该进程无法访问该文件”
- 利用Java.lang.Process和ProcessBuilder创建本地应用程序进程
- VS使用进程调试出现“没有正确安装调试器。请运行安装程序安装或修复调试器”的错误,解决方法。
- 安装sybase出现 找不到JVM错误请使用选项 -is:javahome<JAVA HOME DIR>