-1
package test_cmd_command;

import java.io.BufferedReader;
import java.io.IOException; 
import java.io.InputStreamReader;
import java.time.LocalDateTime;


public class CommandLine {

public static String executeCommand(String cliCommand) {
    String s = null;
    BufferedReader stdInput = null;
    BufferedReader stdError = null;
    String error = "";
    String output = "";
    try {
        ProcessBuilder pb1 = new ProcessBuilder(
                "bash",
                "-c",
                cliCommand);

        pb1.redirectErrorStream(true);
        Process p = pb1.start();

        stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        while ((s = stdInput.readLine()) != null) {
            output += "\n" + s;
        }
        //System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            //System.out.println(">> "+s.toString());
            error += "\n" + s;
        }

    } catch (IOException e) {
        System.out.println("exception happened - here's what I know: \n" + e.getMessage());
    } finally {
        try {
            stdInput.close();
            stdError.close();
        } catch (IOException e) {

        }
    }
    String returnValue = null;
    if (output != null && error != null) {
        returnValue = output + "\n" + ":  " + error;
    } else if (output != null) {
        returnValue = output;
    }
    return returnValue;
}
}

"csc -version" is running in terminal but not from my java program on MAC. it give Output "bash Command Not Found". Is any way to solve this issue.... This program run other commands correctly like javac -version etc. I am running this program on MAC not on windows.

Hamza Shahid Ali
  • 224
  • 4
  • 14
  • I guess there is no "cmd" on MacOS' bash? In the bash you run "csc -version", but in your code you actually run in a bash "cmd /c csc -version" ... Have you tried `Runtime.getRuntime().exec("cliCommand);` ? Just a guess, not familiar with MacOS. – Fildor Oct 18 '18 at 11:24
  • Possible duplicate of [Open a Terminal window with Java](https://stackoverflow.com/questions/23956389/open-a-terminal-window-with-java) – Access Denied Oct 18 '18 at 11:42
  • @AccessDenied Its not working for me.. – Hamza Shahid Ali Oct 18 '18 at 12:36

1 Answers1

1

This work for me

export PATH=/Library/Frameworks/Mono.framework/Versions/Versions/bin/:${PATH}

I run command like this "export PATH=/Library/Frameworks/Mono.framework/Versions/Versions/bin/:${PATH}; csc -version" and it works and return version of csc.

Hamza Shahid Ali
  • 224
  • 4
  • 14