2

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?

2
  • 1
    are you running your program in command line or you are using an IDE to run it? Commented Jan 28, 2020 at 23:12
  • Hi, I am running my program in command line , when i am using IDE i am redirecting using eclipse> Run configuration> output file but when i execute via command linei am not aware of redirecting it to an external file. Even in java program if we can redirect entire console log to output file will solve my problem. System.set out only save when we use system.out.println but i want entire console output Commented Jan 28, 2020 at 23:15

3 Answers 3

3

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();
  }
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend creating that buffered writer using Files.newBufferedWriter(...) and then get rid of the File.
Also, you might want to consider using try-with-resources to make sure the file handle is closed properly.
1

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.

Comments

0

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 /"));
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.