Brian Allison
Brian Allison

Reputation: 9

Send some input to a Terminal window using Java on Mac OS

I can get a terminal window or command prompt to open on either Mac OS or Windows. I want to send a string to that terminal or cmd window using my java.

  String in = " -i " + "\"" + tfIntdta.getText() + "\"";
  String rst = " - r " + "\"" + tfRstplt.getText() + "\"";
  String out = " -o " + "\"" + tfOutdta.getText() + "\""; 
  String strip = " -s" + "\"" + tfStpdta.getText() + "\"";
  String guistring = "-n gui";
  String wd = "\"" + System.getProperty("user.dir");
  String osver = System.getProperty("os.name");
  String app = "";
    if (osver.contains("Mac")){
       app = wd + "/relap5.x\"";
    } else if (osver.contains("Windows")){
       app = "\\relap5.exe";
    } else if (osver.contains("linux")) {
       app = "/relap5.x";
    }
 String run = app + in + rst + out;

So the string would look something like this. "/Users/brianallison/Documents/Java/RELAP5 GUI/issrs/relap5.x" -i "" - r "" -o ""

I want the line above to appear on the terminal or cmd window and execute.

Upvotes: 1

Views: 705

Answers (1)

Roger Gustavsson
Roger Gustavsson

Reputation: 1719

Put your command and parameters in an array:

String[] command = {
    "/Users/brianallison/Documents/Java/RELAP5 GUI/issrs/relap5.x",
    "-i", "Choose your input file",
    "-r", "",
    "-o", ""
};

Then execute using Runtime#exec(String[] cmdarray):

Runtime.getRuntime().exec(command);

This answer has been put together after reading your other two questions from today, here and here.

Upvotes: 1

Related Questions