问题描述
我可以在 Mac OS 或 Windows 上打开终端窗口或命令提示符.我想使用我的 java 向该终端或 cmd 窗口发送一个字符串.
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;
所以字符串看起来像这样."/Users/brianallison/Documents/Java/RELAP5 GUI/issrs/relap5.x" -i "" - r "" -o ""
So the string would look something like this."/Users/brianallison/Documents/Java/RELAP5 GUI/issrs/relap5.x" -i "" - r "" -o ""
我希望上面的行出现在终端或 cmd 窗口中并执行.
I want the line above to appear on the terminal or cmd window and execute.
推荐答案
把你的命令和参数放在一个数组中:
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", ""
};
然后使用 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.
这篇关于在 Mac OS 上使用 Java 将一些输入发送到终端窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!