您的位置:首页 > 运维架构 > Shell

通过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());

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: