简体   繁体   中英

How to Print entire Command line output to an external file using Java

I want to save the Entire command line logs for a command executed using java, which i am using to send it to a different program via an API. In Eclipse we can achieve this via Run Time Configuration by setting the output file. So , is there a way we can send the entire output from a command line execution and save it in external file?

Basic example if you are running a command through java code

public static void main(String[] argv) throws Exception {
    String command = "ls";
    Process child = Runtime.getRuntime().exec(command);
    InputStream in = child.getInputStream();
   BufferedReader br =
                    new BufferedReader(new InputStreamReader(in));

BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Filepath")));
String line;
while((line=br.readLine())!=null){

      bw.write(line);
      bw.newLine();
}

bw.close();
  }

The java application only knows about the arguments passed to it; it knows nothing about JVM options, dynamic environment settings (ie -Dname=value ) etc.

To see the entire command, you would have to use an OS command to look at the running processes and examine its output.

Eg in linux, use long pid = ProcessHandle.current().pid(); then execute "ps" with args "-p", pid then parse the output.

My two cents elaborating on the answer of Sandeep Patel.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

public class Os {

  public static boolean exec(String command, String filePath) {
    try (InputStream in = Runtime.getRuntime().exec(command).getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath)))) {

      String line;
      while ((line = br.readLine()) != null) {
        bw.write(line);
        bw.newLine();
      }
      return true;

    } catch (IOException e) {
      return false;
    }
  }

  public static String exec(String command) {
    try (InputStream in = Runtime.getRuntime().exec(command).getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringWriter sw = new StringWriter()) {

      String line;
      while ((line = br.readLine()) != null)
        sw.write(line + "\n");

      return sw.toString();

    } catch (IOException e) {
      return null;
    }
  }

  public static String exec2(String command) {
    try (InputStream in = Runtime.getRuntime().exec(command).getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in))) {

      String        line;
      StringBuilder sb = new StringBuilder();
      while ((line = br.readLine()) != null)
        sb.append(line).append("\n");
      return sb.toString();

    } catch (IOException e) {
      return null;
    }
  }

  public static void main(String[] argv) {
    System.out.println(Os.exec2("ls -Fla /"));
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM