java基础/java调用shell命令和脚本
一。项目需求:
从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书。
问题:linux服务器启动该服务,不能关闭。一旦关闭,服务即停止。
解决方案:java调用shell命令,利用spring容器启动即执行方案。
参考博文:http://zohan.iteye.com/blog/1709136
项目结构:

原码:
1。RuntimeUtils.java
package com.csvalue.common; import org.springframework.util.StringUtils; import java.io.File;
import java.io.IOException; /*
* java调用sheet脚本工具类
* */
public class RuntimeUtils {
public static Process exec(String command, String envp, String dir)
throws IOException {
String regex = "\\s+";
String args[] = null;
String envps[] = null;
if (!StringUtils.isEmpty(command)) {
args = command.split(regex);
}
if (!StringUtils.isEmpty(envp)) {
envps = envp.split(regex);
}
return Runtime.getRuntime().exec(args, envps, new File(dir));
}
}
2。p10Server.java
package com.csvalue.service.impl; import com.csvalue.common.RuntimeUtils;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; /*
* Spring容器启动即启动sh脚本文件
* */
@Service
public class P10Server {
final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
@PostConstruct
public void init(){
System.out.println("启动p10服务:init....");
String command = "sh startup.sh";
String dir=RuntimeUtils.class.getResource("/").getPath()+"KTServer";
System.out.println(dir);
try {
RuntimeUtils.exec(command, null, dir);
//Process process = RuntimeUtils.exec(command, null, dir);
//int i = process.waitFor();
// System.exit(i);
}catch (Exception e){
log.error("启动p10异常:"+e.getMessage());
} }
}
注意:p10Server.java中注释掉的部分。若开启,则服务一直启动不起来!!!
java基础/java调用shell命令和脚本的更多相关文章
- Hadoop概念学习系列之Java调用Shell命令和脚本,致力于hadoop/spark集群(三十六)
前言 说明的是,本博文,是在以下的博文基础上,立足于它们,致力于我的大数据领域! http://kongcodecenter.iteye.com/blog/1231177 http://blog.cs ...
- java调用shell命令及脚本
shell脚本在处理文本及管理操作系统时强大且简单,将shell脚本结合到应用程序中则是一种快速实现的不错途径本文介绍使用java代码调用并执行shell 我在 -/bin/ 目录下写了jbossLo ...
- Java基础-Java中23种设计模式之常用的设计模式
Java基础-Java中23种设计模式之常用的设计模式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.设计模式分类 设计模式是针对特定场景给出的专家级的解决方案.总的来说设 ...
- java基础---->java中正则表达式二
跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...
- python 调用shell命令三种方法
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...
- python 调用shell命令的方法
在python程序中调用shell命令,是件很酷且常用的事情…… 1. os.system(command) 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出 ...
- Python 调用 Shell命令
python程序中调用shell命令,是件很酷且常用的事情今天来总结一下 1.使用os模块 的 system 此函数会启动子进程,在子进程中执行command,并返回comman ...
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- Awk中调用shell命令
Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...
随机推荐
- 怎么画一条0.5px的边
编者按:本文由人人网FED发表于掘金,并已授权奇舞周刊转载 什么是像素? 像素是屏幕显示最小的单位,在一个1080p的屏幕上,它的像素数量是1920 1080,即横边有1920个像素,而竖边为1080 ...
- <el-tree>文字显示不全解决办法(可以用省略号代替)
地址: https://www.jianshu.com/p/229f96b794d3
- Python---webserver项目
# HTTP项目实战 - 深入理解HTTP协议 - 模拟后台服务程序基本流程和大致框架 - 每一个步骤一个文件夹 - 图解http协议,图解tcp/ip协议 # v01-验证技术 - 验证socket ...
- JAVA笔记17-容器之一 图、Collection接口、Iterator接口(重要)
掌握1136: (1)1个图 (2)1个类:Collections (3)3个知识点:For(了解),Generic泛型,Auto-boxing/unboxing(自动打包/解包) (4)6个接口:C ...
- springboot 在idea中实现热部署
SpringBoot的web项目,在每一次修改了java文件或者是resource的时候,都必须去重启一下项目,这样的话浪费了很多的时间,实现了热部署,在每一次作了修改之后,都会自动的重启 第一步:引 ...
- git的用法的总结
git的基本用法(一) 1).git的初始化 --配置用户名和邮箱 不然后面无法提交 git config --global user.name "myname"; git con ...
- js-计算textarea可剩余的字数???
效果图 HTML: JS:
- TCP,UDP,HTTP
使用图表非常系统介绍了TCP和UDP的区别 https://blog.fundebug.com/2019/03/22/differences-of-tcp-and-udp/ 举了一个TCP/IP通讯的 ...
- Maven构建生命周期
以下引用官方的生命周期解释https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html: 一.构建生命 ...
- android系统时间格式转换工具类
代码依旧非常简单,只不过因为这个方法极为常用,因此体现的还是封装的思想. package com.ctbri.weather.utils; import java.text.SimpleDateFor ...