通过JAVA在命令行(如控制台)运行Shell指令
2016-04-01 10:12
399 查看
package com.things.boring.runtime;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class RuntimeTest {
/**
* Run a command in the command line just like the console.
*/
public static void main(String[] args) throws IOException {
Process process = null;
Runtime rt = Runtime.getRuntime();
try {
process = rt.exec("ls -la");
} catch (IOException e) {
e.printStackTrace();
}
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String b;
while((b=br.readLine())!=null){
System.out.println(b);
System.out.println(br.readLine());
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class RuntimeTest {
/**
* Run a command in the command line just like the console.
*/
public static void main(String[] args) throws IOException {
Process process = null;
Runtime rt = Runtime.getRuntime();
try {
process = rt.exec("ls -la");
} catch (IOException e) {
e.printStackTrace();
}
try {
process.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String b;
while((b=br.readLine())!=null){
System.out.println(b);
System.out.println(br.readLine());
}
}
}
相关文章推荐
- 在WinDBG中, 使用.shell命令来搜索字符串
- Linux shell 之 提取文件名和目录名的一些方法
- xshell 连接到虚拟机
- 批量远程执行shell命令工具
- 怎么获得webshell asp/PHP环境搭建 2016.03.31回顾
- Ubuntu更换bash为zsh
- bash逐行读取文件内容
- shell学习日记2
- SHELL学习日记1
- Xshell实现Windows上传文件到Linux主机
- 对log进行shell方法处理
- 登录式shell的执行顺序及简单应用
- shell 之 bash 与sh 区别
- bash算术求值和errexit陷阱
- shell脚本相关
- 2款XSHELL配色方案及导入配色方案的方法
- HDU2149——Public Sale(Bash Game巴什博奕)
- Linux Shell 学习笔记1:Shell程序的运行及基本介绍
- ORMLite的几个关键注解的使用说明
- 有关bash的一些常识