java中的路径包含空格问题
2012-12-17 13:02
267 查看
原文地址: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);
相关文章推荐
- Java中解决访问地址中包含空格和中文路径的问题
- java中的路径包含空格问题
- Java环境变量配置问题:路径包含空格或特殊字符(_,~等)
- Java环境变量配置问题:路径包含空格或特殊字符(_,~等)
- java中获取路径中的空格处理(%20)问题
- JAVA路径中空格问题
- java中获取路径中的空格处理(%20)问题
- Java路径空格问题
- cmd路径包含空格时易出现的问题及其解决方案
- Java获取路径 空格问题
- java 路径中空格问题
- 【JAVA】关于java中 类.class.getResource("/").getPath()获取路径有空格的问题
- Java路径中的空格问题
- 包含空格的项目的文件/路径部分需要用括号 问题解决方案
- 20110321-Java或者JDK/JRE路径中包含空格的处理方法
- 解决IIS出现包含空格的项目的文件/路径部分需要用括号括起来的问题
- Ant 执行 exec cmd.exe 时路径包含空格的问题
- 整理java文件下载(中文乱码、火狐浏览器乱码、空格变+号、文件名包含空格时文件名可能显示不全等问题)
- java中获取路径中的空格处理(%20)问题
- 转:Windows中的命令行提示符里的Start命令执行路径包含空格时的问题