出现问题:
此时不应有 Files\Java\jdk1.8.0_92\bin\java.exe""(本地未出现问题,服务器环境出现)
出现原因:
1:环境变量JAVA_HOME配置路径中的空格问题
2:系统环境变量路径首尾出现双引号 " 为多余
解决方法:
1:原路径C:\Program Files\Java\jdk1.8.0_92,需替换为C:\Progra~1\Java\jdk1.8.0_92 ;
2:将相关环境变量路径的首尾双引号 “ 去掉 ;
如:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public static Map<String, String> execute(String directory,String[] cmd){
Map<String, String> result = new HashMap<>();
Process p = null;
try {
ProcessBuilder builder = new ProcessBuilder(cmd);
File file = new File(directory) ;
builder.directory(file);
Map<String, String> env = builder.environment();
env.put("CATALINA_HOME", file.getParent());
env.put("CATALINA_BASE", file.getParent());
logger.info("=: -----------------------");
for(String str : env.keySet()){
if(env.get(str) != null){
env.put(str, env.get(str).replaceAll("Program Files", "PROGRA~1"));
}
logger.info(str+"=: "+env.get(str));
}
logger.info("=: -----------------------");
String javaHome = System.getenv("JAVA_HOME");
if(StringUtils.isNotEmpty(javaHome)) {
javaHome = javaHome.replace("%20", " ").replaceAll("Program Files", "PROGRA~1");
}
env.put("JAVA_HOME", javaHome);
//环境可能是Path 或 PATH
String _Path = env.get("Path");
if(StringUtils.isNotEmpty(_Path)){
env.put("Path", _Path.replaceAll("\"", ""));
}
String _PATH = env.get("PATH");
if(StringUtils.isNotEmpty(_PATH)){
env.put("PATH", _PATH.replaceAll("\"", ""));
}
String _EXECJAVA = env.get("_EXECJAVA");
if(StringUtils.isNotEmpty(_EXECJAVA)){
env.put("_EXECJAVA", _EXECJAVA.replaceAll("\"", ""));
}
String _RUNJAVA = env.get("_RUNJAVA");
if(StringUtils.isNotEmpty(_RUNJAVA)){
env.put("_RUNJAVA", _RUNJAVA.replaceAll("\"", ""));
}
String _RUNJDB = env.get("_RUNJDB");
if(StringUtils.isNotEmpty(_RUNJDB)){
env.put("_RUNJDB", _RUNJDB.replaceAll("\"", ""));
}
for(String str : env.keySet()){
logger.info(str+"=: "+env.get(str));
}
logger.info("正在执行命令...." + builder.directory());
p = builder.start();
logger.info("正在执行命令....");
InputStream errorStream = p.getErrorStream();
ExecutorService exec = Executors.newCachedThreadPool();
Future<String> errorSubmit = exec.submit(new WorkCallable(p, errorStream,"ERROR"));
InputStream correctStream = p.getInputStream();
Future<String> correctSubmit = exec.submit(new WorkCallable(p, correctStream,"STDOUT"));
logger.info("正在执行命令2....");
String errorMsg = errorSubmit.get();
String successMsg = correctSubmit.get();
logger.info(errorMsg);
logger.info(successMsg);
if(StringUtils.isEmpty(errorMsg)) {
result.put("status", "success");
result.put("msg", successMsg);
} else {
result.put("status", "error");
result.put("msg", errorMsg);
}
p.waitFor();
exec.shutdown();
} catch (Exception e) {
result.put("status", "error");
result.put("msg", e.getMessage());
logger.error("执行命令异常,请检查参数",e.getMessage());
}
return result;
}
欢迎工作一到五年的Java工程师朋友们加入Java程序员开发: 721575865
群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!