一。项目需求:

从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书。

问题:linux服务器启动该服务,不能关闭。一旦关闭,服务即停止。

解决方案:java调用shell命令,利用spring容器启动即执行方案。

参考博文:http://zohan.iteye.com/blog/1709136

项目结构:

java基础/java调用shell命令和脚本

原码:

1。RuntimeUtils.java

 1 package com.csvalue.common;
 2 
 3 import org.springframework.util.StringUtils;
 4 
 5 import java.io.File;
 6 import java.io.IOException;
 7 
 8 /*
 9 * java调用sheet脚本工具类
10 * */
11 public class RuntimeUtils {
12     public static Process exec(String command, String envp, String dir)
13             throws IOException {
14         String regex = "\\s+";
15         String args[] = null;
16         String envps[] = null;
17         if (!StringUtils.isEmpty(command)) {
18             args = command.split(regex);
19         }
20         if (!StringUtils.isEmpty(envp)) {
21             envps = envp.split(regex);
22         }
23         return Runtime.getRuntime().exec(args, envps, new File(dir));
24     }
25 }
View Code

相关文章: