java 路径批处理空格 getRuntime().exec 空格解决办法

本文转自:http://blog.sina.com.cn/s/blog_5427beb00100ga7i.html

1.java类路径空格


通过 Java代码
String   configPath=this.getClass().getResource("/").toString  

String  configPath=this.getClass().getResource("/").toString
这种方式获取的路径,其中的空格会被使用“%20”代替,只要使用
Java代码
String   configPath=this.getClass().getResource("/").toString().replaceAll("%20", " ");  

String  configPath=this.getClass().getResource("/").toString().replaceAll("%20", " ");
就可以解决。

2、在批处理文件中如果,命令中含有空格,如下:
Java代码
set JAVA_JRE=D:/Program Files/tece2.1/jre  
set CATALINA_HOME=D:/Program Files/tece2.1/  
call     %CATALINA_HOME%bin/service    install  
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代码
%CATALINA_HOME%bin/service  

%CATALINA_HOME%bin/service
中将含有空格,批处理文件将无法执行,需要在整个命令前后加双引号如下:

Java代码
set JAVA_JRE=D:/Program Files/tece2.1/jre  
set CATALINA_HOME=D:/Program Files/tece2.1/  
call   " %CATALINA_HOME%bin/service"    install  
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代码
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代码
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代码
String   commandStr="cmd.exe /c"+" " +realPath.realTomcatPath.replace(" ", "\" \"");    

String  commandStr="cmd.exe /c"+" " +realPath.realTomcatPath.replace(" ", "\" \"");

Java代码
Runtime.getRuntime().exec(commandStr);  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值