Java调用Linux命令(cd的处理)
一、Java调用Linux系统的命令非常简单
这是一个非常常用的调用方法示例:
public String executeLinuxCmd(String cmd) {
System.out.println("got cmd job : " + cmd);
Runtime run = Runtime.getRuntime();
try {
Process process = run.exec(cmd);
InputStream in = process.getInputStream();
BufferedReader bs = new BufferedReader(new InputStreamReader(in));
// System.out.println("[check] now size \n"+bs.readLine());
StringBuffer out = new StringBuffer();
byte[] b = new byte[8192];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
System.out.println("job result [" + out.toString() + "]");
in.close();
// process.waitFor();
process.destroy();
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
二、含有管道符(|)多级命令串联查询
public List<String> executeLinuxCmd(String cmd) {
System.out.println("got cmd job : " + cmd);
Runtime run = Runtime.getRuntime();
try {
// Process process = run.exec(cmd);
Process process = run.exec(new String[] {"/bin/sh", "-c", cmd});
InputStream in = process.getInputStream();
BufferedReader bs = new BufferedReader(new InputStreamReader(in));
List<String> list = new ArrayList<String>();
String result = null;
while ((result = bs.readLine()) != null) {
System.out.println("job result [" + result + "]");
list.add(result);
}
in.close();
// process.waitFor();
process.destroy();
return list;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
三、含有cd操作的方法示例
1. 问题背景
1.1 java程序运行在/home/lings目录下;
1.2 希望删除/home/test目录下的文件proxy.log;
1.3 调用上面的接口两次?
executeLinuxCmd("cd /home/test");
executeLinuxCmd("rm -fr /home/proxy.log");
是不行的!
1.4 这个接口的调用是单次事务型的,就是每次调用都是独立的事务或者说操作,没有关联的。
那这种“复杂”一点的操作流程怎么办呢?
1.5 方法a: 可以写一个独立的脚本,然后一次运行脚本,这样多复杂的逻辑都没问题。
1.6 方法b: 可以启动一个shell长连接,保持连接,发送多条命令,最后释放连接。
示例逻辑代码:
public void executeNewFlow() {
Runtime run = Runtime.getRuntime();
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = run.exec("/bin/bash", null, wd);
} catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd /home/test");
out.println("pwd");
out.println("rm -fr /home/proxy.log");
out.println("exit");//这个命令必须执行,否则in流不结束。
try {
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}
}
三的优化和演进(返回值)
public List<String> executeNewFlow(List<String> commands) {
List<String> rspList = new ArrayList<String>();
Runtime run = Runtime.getRuntime();
try {
Process proc = run.exec("/bin/bash", null, null);
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
for (String line : commands) {
out.println(line);
}
// out.println("cd /home/test");
// out.println("pwd");
// out.println("rm -fr /home/proxy.log");
out.println("exit");// 这个命令必须执行,否则in流不结束。
String rspLine = "";
while ((rspLine = in.readLine()) != null) {
System.out.println(rspLine);
rspList.add(rspLine);
}
proc.waitFor();
in.close();
out.close();
proc.destroy();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return rspList;
}
Java调用Linux命令(cd的处理)的更多相关文章
- Java调用Linux命令执行
调用方式 Java调用linux命令执行的方式有两种,一种是直接调用linux命令,一种是将linux命令写到.sh脚本中,然后调用脚本执行. 详细说明 直接调用:使用java中lang包下面的Run ...
- java调用Linux命令报错:java.io.IOException: Cannot run program "ps": CreateProcess error=2, ?????????
在idea里面,java代码:Runtime.getRuntime().exec("ps -aux") 是因为默认是用windows平台运行了,所以报错,得改成调用Linux平台运 ...
- java 调用 linux 命令行 +使用管道、awk等命令进行数据处理的方法
这里用 sh -c "命令" 的方式是因为java里只能这么用,管道这边java处理不了,所以只能一次执行一条命令,但是在linux里用 sh -c 的方式返回的awk处理过的结果 ...
- Java调用Linux命令
// int tp = 1 返回执行结果 非1 返回命令执行后的输出 public static String runCommand(String cmd, int tp) { StringBuff ...
- java调用Linux执行Python爬虫,并将数据存储到elasticsearch--(环境脚本搭建)
java调用Linux执行Python爬虫,并将数据存储到elasticsearch中 一.以下博客代码使用的开发工具及环境如下: 1.idea: 2.jdk:1.8 3.elasticsearch: ...
- java基础/java调用shell命令和脚本
一.项目需求: 从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书. 问题:linux服务器启动该服务,不能关闭.一旦关闭,服务即停止. 解决方案:java ...
- php通过system()调用Linux命令问题
最近在做php和linux crontab的联调,发现php在linux下的权限问题需要引起注意,调试问题的过程中发现有许多问题前人说的比较零散,我在这里汇总,顺带抛砖引玉一下. 1.$result= ...
- scala调用Linux命令行
在 scala 里面存在 调用 Linux 命令行的函数: import scala.sys.process._ 执行的方法也不难: import scala.sys.process._ /** * ...
- Java调用windows命令
JAVA调用windows的cmd命令 用起来会让程序变得更加简洁明了,非常实用. 核心就是使用 Runtime类. cmd的xcopy就有很强大的文件夹,文件处理功能. 下面就以xcopy来说明,如 ...
随机推荐
- spring boot测试工具(自带)
启动spring boot 项目(一般是openapi) http://localhost:8888/swagger-ui.html 端口号可以自己配
- [NPM] Use npx to run commands with different Node.js versions
We will use npx to run a package using different versions of Node.js. This can become valuable when ...
- (LeetCode 21)Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- ArcEngine真正释放锁文件,彻底移除图层
ArcMap在加载图层时会自动生成一个lock格式的加锁文件,右击移除图层后,加锁文件也会自动删除.但AE开发中却不能正常删除,移除图层后加锁文件依然存在,这就导致在其他地方无法对该图层进行操作,只有 ...
- HTTP.SYS 远程执行代码漏洞分析(MS15-034 )
在2015年4月安全补丁日,微软发布了11项安全更新,共修复了包括Microsoft Windows.Internet Explorer.Office..NET Framework.Server软件. ...
- 把Linux目录挂载到开发板、设置开发板从NFS启动、取消开发板从NFS启动
声明:文中"PC虚拟机Linux"是指在PC上安装了虚拟机,然后在虚拟机中装的Linux. 关于NFS的详细介绍可参考:http://www.cnblogs.com/nufangr ...
- 移动端网页宽度值(未加meta viewport标签)
移动端网页宽度值(未加meta viewport标签): iphone:980px Galaxy(盖乐世):980px Nexus:980px blackberry(黑莓):980px LG:980p ...
- Windows下安装OpenSSL及其使用
方法一: Windows binaries can be found here: http://www.slproweb.com/products/Win32OpenSSL.html You can ...
- apachebench的简单使用
apachebench的简单使用 2013-03-08 15:48:47 分类: LINUX ApacheBench是 Apache 附带的一个小工具,专门用于 HTTP Server 的benchm ...
- spring cloud单点登录
概述 基于springcloud的单点登录服务及基于zuul的网关服务(解决了通过zuul转发到认证服务之后session丢失问题) 详细 代码下载:http://www.demodashi.com/ ...