zoukankan      html  css  js  c++  java
  • 命令行运行java

    运行TestLinuxCommand.java
     
     
    代码
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
     
    /**
    *
    * @author nathan
    */
    public class TestLinuxCommand {
         public static void main(String[] args) {
             String logDir = System.getProperty("log.dir");
            
             //windows
    //         String commands = "cmd.exe /c c: && dir";
            
    //         String commands = "ls -l";
    //         String commands = "sh -c cd /data/log/daemon/commonprocess && ls -lt";
             String[]   commands   =   new   String[]   {"/bin/sh", "-c", "/bin/ls -lt /data/log/daemon/commonprocess"};
    //         String[] commands = new String[]{"cmd","dir"};
            
             java.lang.Process process = null;        
         try
              {
                  process = Runtime.getRuntime().exec(commands);
                  System.out.println("exec commands success");
                  InputStreamReader inputStream = new InputStreamReader(process.getInputStream());
                  BufferedReader input = new BufferedReader(inputStream);
                  String line;
                  System.out.println("print inputStream start");
                  while ((line = input.readLine()) != null){
                       System.out.println(line);
                  }
                  System.out.println("print inputStream over");
                 
                  InputStreamReader errorStream = new InputStreamReader(process.getErrorStream());
                  input = new BufferedReader(errorStream);
                  System.out.println("print errorStream start");
                  while ((line = input.readLine()) != null){
                       System.out.println(line);
                       input.close();
                  }
                  System.out.println("print errorStream over");
            
              } catch (IOException e)
              {
                   System.out.println("have ioexception");
                   e.printStackTrace();
              } finally{
                   try
                   {
                        System.out.println(process.waitFor());
                   } catch (InterruptedException e)
                   {
                        e.printStackTrace();
                   }
              }
         }
    }
     
     
     
    命令
    java -classpath .:/usr/java/jdk1.6.0_26/lib/* TestLinuxCommand

    javac TestLinuxCommand.java 
  • 相关阅读:
    随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比
    stringstream读入每行数据
    java Log4j封装,程序任何位置调用
    Oracle 归档模式和非归档模式
    为什么需要 RPC 服务?
    JFrame windowbuiler的使用基础
    Eclipse安装windowsbuilder
    字符串反转
    static{}静态代码块与{}普通代码块之间的区别
    jQuery EasyUI 数据网格
  • 原文地址:https://www.cnblogs.com/svennee/p/4082878.html
Copyright © 2011-2022 走看看