Runtime.getRuntime().exec中命令含有括号问题

在写批量执行bat工具的时候,想起了之前写的定时小工具里面的执行方法。

使用Runtime.getRuntime().exec方法。

Runtime.getRuntime().exec("cmd /c start c:/test.bat") 

这样就可以像dos窗口直接执行命令行一样。

getRuntime返回Runtime的实例化对象,exec方法是返回Process对象。

查看Process类可以发现:

The ProcessBuilder.start() and Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods
for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.

The methods that create processes may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts.

方法创建了本地进程返回了Process子类的对象,用于控制进程并获取信息。Process类提供从进程输入,进程输出,进程等待结束和进程退出状态还有销毁(杀死)进程的方法。创建进程的方法对于本地平台的特殊进程可能无法较好地工作。如本地窗口进程,守护进程,微软Windows下的Win16/DOS 进程或shell脚本。

先在c盘写一个简单的test.bat。

echo aaaa
pause

然后是用java进行调用:

import java.io.IOException;

public class TestRuntime {
	public static void main(String[] args) {
		try {
			Runtime.getRuntime().exec("cmd /c start c:\\test.bat");
		} catch (IOException e) {
			// TODO Auto-generated catch block			System.out.println("file not found");
		}
	}
}

这样是正常执行的。

但是问题来了,当bat文件有括号的时候,执行就会不一样了,将原有的test.bat改为test(s).bat后执行。

后面看到了这篇博客,http://blog.sina.com.cn/s/blog_656977f40101d05p.html

但是博客里面也有错误,文件路径应该使用\\或者/。

Runtime.getRuntime().exec("cmd /c start call \"c:\\test(s).bat\"");

这样之后正常访问,call正常调用了带括号bat文件。

其实这个问题纠结了我很久,确实有些命令并不能很好的工作,一直就在解决这个问题。但是我还好找到了这个问题的解决方案,不然只会在这个问题上越陷越深,其实当一个问题快到死角的时候,要从换种思路解决问题,

原来的思路:

现在问题是无法识别文件名带有括号的bat文件——》如何让exec方法识别到。

新思路:

现在问题是无法识别文件名带有括号的bat文件——》能不能去掉bat文件名中的括号。

其实这个思路是由这位仁兄的博客里想到的:

http://blog.csdn.net/xulianboblog/article/details/18360131

不一样的解决问题的思路。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 06:35:03

Runtime.getRuntime().exec中命令含有括号问题的相关文章

使用Runtime.getRuntime().exec()在java中调用python脚本

举例有一个Python脚本叫test.py,现在想要在Java里调用这个脚本.假定这个test.py里面使用了拓展的包,使得pythoninterpreter之类内嵌的编译器无法使用,那么只能采用java调用控制台进程,即 Runtime.getRuntime().exec(),来运行这个python脚本. 在windows下运行这个程序,假如是参考了一些百度来的被转载了无数遍的文章,很有可能运行结果是根本没有执行这个脚本.经过测试,在java中执行如下代码可以成功运行test.py文件: --

Java Runtime.getRuntime().exec 执行带空格命令解决办法

String command = OpenOffice_HOME + "program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard "; command = "cmd /c start "+command.replaceAll(" ","\" \""); P

java中Runtime.getRuntime().exec()的坑,会造成阻塞的解决

synchronized (this) { process = Runtime.getRuntime().exec(cmds); } //记录进程缓存错误信息 final StringBuffer errorLog = new StringBuffer(); //获取执行进程的错误流 final InputStream errorStream = process.getErrorStream(); final InputStream inputStream = process.getInputS

Runtime.getRuntime.exec()执行linux脚本导致程序卡死有关问题

Runtime.getRuntime.exec()执行linux脚本导致程序卡死问题问题: 在Java程序中,通过Runtime.getRuntime().exec()执行一个Linux脚本导致程序被挂住,而在终端上直接执行这个脚本则没有任何问题.原因: 先来看Java代码: public final static void process1(String[] cmdarray) {        Process p = null;        BufferedReader br = null

Runtime.getRuntime().exec方法

Runtime.getRuntime().exec共有四个重载方法: public Process exec(String command) 在单独的进程中执行指定的字符串命令. public Process exec(String [] cmdArray) 在单独的进程中执行指定命令和变量 public Process exec(String command, String [] envp) 在指定环境的独立进程中执行指定命令和变量 public Process exec(String []

Eclipse下使用Runtime.getRuntime().exec启动ja

window7系统下实验(linux下路径格式和windows下不一样) Eclipse下使用Runtime.getRuntime().exec启动ja RunTime.getRuntime().exec("java My_Program");System.exit(0); which would start a whole new JVM running your program, and then kill the original. Not exactly pretty or e

Runtime.getRuntime().exec()----记录日志案例

Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令. Runtime.getRuntime().exec共有六个重载方法: 1.public Process exec(String command) 在单独的进程中执行指定的字符串命令. 2.public Process exec(String [] cmdArray) 在单独的进程中执行指定命令和变量 3.public Process exec(String command, String [] envp)

通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本

Android Runtime使得直接调用底层Linux下的可执行程序或脚本成为可能 比如Linux下写个测试工具,直接编译后apk中通过Runtime来调用 或者写个脚本,apk中直接调用,省去中间层或者JNI 这个至少效率应该比较高吧 代码: [java] view plaincopy public class test extends Activity { TextView text; /** Called when the activity is first created. */ @O

用Runtime.getRuntime().exec()需要注意的地方

d有时候我们可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回一个java.lang.Process对象,该对象可以得到之前开启的进程的运行结果,还可以操作进程的输入输出流. Process对象有以下几个方法: 1.destroy() 杀死这个子进程 2.exitValue() 得到进程运行结束后的返回状态 3.waitFor() 得到进程运行结束后的返回状态,如果进程未运行完毕则等待知道执