1

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.

Jim Fell
  • 13,750
  • 36
  • 127
  • 202
  • 1
    Can't you just execute the string directly? Check `Runtime`'s javacod, you're probably interested in `Runtime.exec(String command)` or one of it's overloads. Fun fact/product placement: in Groovy, `String` has an `execute` method that runs the String as a terminal command. – CptBartender May 08 '15 at 15:03
  • I have tried this and all it does is open the terminal window. Desktop.getDesktop().open(new File("/Applications/Utilities/Terminal.app")); Runtime.getRuntime().exec(run); – Brian Allison May 08 '15 at 15:36
  • 1
    Skip the terminal opening - just execute the string, you don't have to explicitly open the terminal. – CptBartender May 08 '15 at 15:51

1 Answers1

1

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.

Community
  • 1
  • 1
Roger Gustavsson
  • 1,689
  • 10
  • 20
  • Ok so I did the above reference and It gives no error, but my program does no open up. If I double click on my .x program it will open up, without using my Java GUI. This is all it shows in the output window of Netbeans. /Users/brianallison/Documents/Java/RELAP5 GUI/issrs/dist/relap5.x -i "/Users/brianallison/issrs/rs34bil.exe.04.30.2014/run/boiloff.i" -r "/Users/brianallison/issrs/rs34bil.exe.04.30.2014/run/boiloff.r" -o "/Users/brianallison/issrs/rs34bil.exe.04.30.2014/run/boiloff.o" – Brian Allison May 11 '15 at 16:15
  • My .x file opens a terminal window. – Brian Allison May 11 '15 at 16:34
  • I'm sorry, but I'm not familiar with MacOS. Perhaps someone else can answer your questions. – Roger Gustavsson May 11 '15 at 18:00
  • Anyone know about mac and Java? – Brian Allison May 15 '15 at 16:21
  • Ask a new question and you will reach the whole community. It's likely only I who will notice your comment. – Roger Gustavsson Jun 05 '15 at 05:57