java 执行linux脚本

Java执行Linux命令代码
java 程序调用linux命令执行语句:RunTime.getRunTime().exec("ls -al");


Java的RunTime.getRunTime().exec("ls -al"); 执行之后无法知道执行结果,因为这个语句返回的是Process进程类的子类实例,因此你要想输出东西的话,那就要用到输入输出流了。

java实例:
—————————————————————————————————————
import java.io.*;
public class Test{

public static void main(String[] args) throws Exception{
try{
   Process process=Runtime.getRuntime().exec("ls ./");
   InputStreamReader reader = new InputStreamReader(process.getInputStream());
   LineNumberReader line = new LineNumberReader(reader);
   String str;
   while((str=line.readLine())!=null){
    System.out.println(str);
   }

   }catch (Exception e){
     e.printStackTrace();
   }
     System.out.println("done !!!");
   }

猜你喜欢

转载自blog.csdn.net/u010786396/article/details/60577271