原文地址:http://blog.csdn.net/congyuandong/article/details/7039965
1.java类路径空格 通过 Java代码 1. String configPath=this.getClass().getResource("/").toString String configPath=this.getClass().getResource("/").toString 这种方式获取的路径,其中的空格会被使用“%20”代替,只要使用 Java代码 1. String configPath=this.getClass().getResource("/").toString().replaceAll("%20", " "); String configPath=this.getClass().getResource("/").toString().replaceAll("%20", " "); 就可以解决。 2、在批处理文件中如果,命令中含有空格,如下: Java代码 1. set JAVA_JRE=D:/Program Files/tece2.1/jre 2. set CATALINA_HOME=D:/Program Files/tece2.1/ 3. call %CATALINA_HOME%bin/service install 4. pause set JAVA_JRE=D:/Program Files/tece2.1/jre set CATALINA_HOME=D:/Program Files/tece2.1/ call %CATALINA_HOME%bin/service install pause 这样命令 Java代码 1. %CATALINA_HOME%bin/service %CATALINA_HOME%bin/service 中将含有空格,批处理文件将无法执行,需要在整个命令前后加双引号如下: Java代码 1. set JAVA_JRE=D:/Program Files/tece2.1/jre 2. set CATALINA_HOME=D:/Program Files/tece2.1/ 3. call " %CATALINA_HOME%bin/service" install 4. pause set JAVA_JRE=D:/Program Files/tece2.1/jre set CATALINA_HOME=D:/Program Files/tece2.1/ call " %CATALINA_HOME%bin/service" install pause 3、Runtime.getRuntime().exec() 路基中含有空格,如下: Java代码 1. Runtime.getRuntime().exec("cmd.exe /c D:\\Program Files\\tece2.1\\tececode\\updateprogram\\updateProgram.exe"); Runtime.getRuntime().exec("cmd.exe /c D:\\Program Files\\tece2.1\\tececode\\updateprogram\\updateProgram.exe"); 这样讲无法执行,需要在空格的前后加上双引号,而不是在整个路径的前后加双引号,如下: Java代码 1. Runtime.getRuntime().exec("cmd.exe /c D:\\Program\" \"Files\\tece2.1\\tececode\\updateprogram\\updateProgram.exe"); Runtime.getRuntime().exec("cmd.exe /c D:\\Program\" \"Files\\tece2.1\\tececode\\updateprogram\\updateProgram.exe"); 或者使用替换方式: Java代码 1. String commandStr="cmd.exe /c"+" " +realPath.realTomcatPath.replace(" ", "\" \""); String commandStr="cmd.exe /c"+" " +realPath.realTomcatPath.replace(" ", "\" \""); Java代码 1. Runtime.getRuntime().exec(commandStr); Runtime.getRuntime().exec(commandStr);