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.

3
  • 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. Commented May 8, 2015 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); Commented May 8, 2015 at 15:36
  • 1
    Skip the terminal opening - just execute the string, you don't have to explicitly open the terminal. Commented May 8, 2015 at 15:51

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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"
My .x file opens a terminal window.
I'm sorry, but I'm not familiar with MacOS. Perhaps someone else can answer your questions.
Anyone know about mac and Java?
Ask a new question and you will reach the whole community. It's likely only I who will notice your comment.

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.