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

送分小仙女□ 提交于 2020-01-16 02:55:10

问题


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.


回答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.



来源:https://stackoverflow.com/questions/30125967/send-some-input-to-a-terminal-window-using-java-on-mac-os

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!