Java Runtime.getRuntime().exec() 执行带空格命令
2015-03-24 13:42
447 查看
可执行文件路径如果包含空格,则在java中不能被获取到。
此时Debug一下,会发现 project=null. project.waitFor 的返回值为1.但是去源路径单击bat文件是可以正常运行的,说明问题出在文件路径上。
将文件路径中的空格用双引号引起来就可以了
原先的代码
修改后的代码
此时Debug一下,会发现 project=null. project.waitFor 的返回值为1.但是去源路径单击bat文件是可以正常运行的,说明问题出在文件路径上。
将文件路径中的空格用双引号引起来就可以了
原先的代码
String batpath = file.getCanonicalPath() + "\\resources\\runTest.bat"; //run bat file Process project = Runtime.getRuntime().exec("cmd.exe /c " + batpath.); int exitcode=project.waitFor(); //kill the process project.destroy(); logger.info(exitcode);
修改后的代码
//run bat file Process project = Runtime.getRuntime().exec("cmd.exe /c " + batpath.replaceAll(" ", "\" \""));
相关文章推荐
- java 利用Runtime.getRuntime().exec()代码调用服务器执行命令
- java执行本地命令Runtime.getRuntime().exec("netstat -an");
- java执行本地命令Runtime.getRuntime().exec("netstat -an");
- 在apk 中执行Runtime.getRuntime().exec adb shell各种命令远程控制其他Android设备(一)
- java 路径 批处理空格 getRuntime().exec 空格解决办法
- Runtime.getRuntime().exec()执行一连串命令阻塞解决
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- java 路径 批处理空格 getRuntime().exec 空格解决办法
- Runtime.getRuntime().exec()执行重定向命令方法
- 如何在JAVA代码中执行 exec master..xp_cmdshell @cmd // 当作SQL语句调用就成了 或者调用 Runtime.getRuntime().exec
- java.lang.Runtime.getRuntime().exec 调用后,等待被调用程序结束后再继续执行
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- 整理java Runtime.getRuntime().exec | 命令
- Java调用Runtime.getRuntime().exec(commandStr)来调用cmd执行指令
- Java 使用Runtime.getRuntime.exec 和 ProcessBuilder 调用外部命令
- 用Runtime.getRuntime().exec(cmd)执行命令
- java Runtime.getRuntime().exec 调用系统脚本/命令注意事项
- Runtime.getRuntime().exec如何执行多行命令
- 解决java使用Runtime.exec执行linux复杂命令不成功问题