-1

We are trying to invoke Linux command history from a Java program. We are able to invoke quite a few commands from Java, including date and ls -l without any issues. However, history is failing with the java.io.IOException. The sample source code follows:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class OSCommand {
    public static void main(String[] args) throws Exception {
        dumpCommandOutput("Date", new String[] {"date"});
        // SUCCEEDS

        dumpCommandOutput("Long Directory Listing", new String[] {"ls", "-l"});
        // SUCCEEDS

        dumpCommandOutput("Command History", new String[] {"history"});
        // FAILS
    }

    protected static void dumpCommandOutput(String label, String[] cmdWithArgs) throws IOException {
        System.out.println("Attempting to execute: " + label);
        
        Process p = Runtime.getRuntime().exec(cmdWithArgs);

        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        
        String str;
        
        while((str = br.readLine()) != null) {
            System.out.println(str);
        }
        br.close();
        System.out.println("\n\n");
    }
}

Exception thrown

Exception in thread "main" java.io.IOException: Cannot run program "history": error=2, No such file or directory
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
        at java.lang.Runtime.exec(Runtime.java:621)
        at java.lang.Runtime.exec(Runtime.java:486)
        at OSCommand.dumpCommandOutput(OSCommand.java:16)
        at OSCommand.main(OSCommand.java:10)
Caused by: java.io.IOException: error=2, No such file or directory
        at java.lang.UNIXProcess.forkAndExec(Native Method)
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
        at java.lang.ProcessImpl.start(ProcessImpl.java:134)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
        ... 4 more
Sandeep
  • 1,245
  • 1
  • 13
  • 33
  • Please [edit] your question to include the complete error message you get from the exception as well as the stacktrace. – Progman Jun 27 '21 at 09:39
  • I'm pretty sure `history` is not a stand-alone program at all, but instead it's a command that your shell can execute. Just like `cd`, `fg` and `bg` are shell commands and can't be found as stand-alone executables. – Joachim Sauer Jun 27 '21 at 10:43

1 Answers1

0

The command "history" is not an installed program in your linux distribution. Instead it is a build-in command available from the shell you are using. Therefore, there is nothing for java to execute with Runtime.getRuntime().exec(). Check the results you get when you use the which command on the arguments for date, ls and history:

linryzen ~ # echo $0
bash
linryzen ~ # which ls
/bin/ls
linryzen ~ # which date
/bin/date
linryzen ~ # which history
which: no history in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:
                      /opt/bin:/usr/lib/llvm/12/bin:/usr/lib/llvm/11/bin:
                      /usr/lib/llvm/9/bin)
linryzen ~ # 
Progman
  • 16,827
  • 6
  • 33
  • 48