菜鸟上路CCLinux 2013-06-25
原文转自:http://dreamthinking.blog.163.com/blog/static/205181171201235103945974/
java 调用 linux shell java 调用 linux .sh
执行shell命令语法如下(显示指定目录下的文件,类似命令依次类推):
Runtime.getRuntime().exec(newString[]{"命令","参数1","参数2"});
其中: command参数第一个command[0]为linux命令,command[1].command[2]...其余项为参数
/** 在/home/thinking//workplace 目录下创建myTest文件夹*/
Runtime.getRuntime().exec(newString[]{"mkdir","/home/thinking/workplace/myTest"});
/** 显示thinking 目录下的文件 */
Runtime.getRuntime().exec(newString[]{"ls","/home/thinking/"});
/** 创建文件 */
Runtime.getRuntime().exec(newString[]{"touch","/home/thinking//workplace/mytest"});
String[] commands =newString[]{"/home/thinking/helloWorld.sh","param1","param2"};
try{
Process process =Runtime.getRuntime().exec(command);
InputStream im = process.getInputStream();
BufferedReader br =newBufferedReader(newInputStreamReader(im));
String line ="";
while((line = br.readLine())!=null){
System.out.println(line);
}
}catch(IOException e){
e.printStackTrace();
}