java 路径 批处理空格 getRuntime().exec 空格解决办法 进度完毕再返回问题
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);
http://flyeagle.javaeye.com/blog/406487
============
进度完毕再返回
放在流的阻塞
英文版:http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
翻译版:http://lang_m.itpub.net/post/4398/71874
通过
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);
http://flyeagle.javaeye.com/blog/406487
============
进度完毕再返回
放在流的阻塞
英文版:http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
翻译版:http://lang_m.itpub.net/post/4398/71874
热门话题 · · · · · · ( 去话题广场 )
- 我的童年阅读记忆 126.8万次浏览
- 童年涂鸦拾遗 144.2万次浏览
- 给童年时期的自己推荐一本书 112.0万次浏览
- 看展记 1.3亿次浏览
- 一人一杯一口入魂的夏日特饮 新话题 · 6756次浏览
- 现代人的“卡夫卡时刻” 544次浏览