【发布时间】:2015-07-07 14:00:46
【问题描述】:
我正在尝试在 netbeans 中编写一个 java GUI 以在命令行上执行程序,并且目前将这段代码分配给一个按钮
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("open -a /Applications/Utilities/Terminal.app");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null)
{
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
这会打开终端,但是我想知道我应该如何在仍然按下按钮的同时将命令输入终端(例如:“ls”、“cd”、“javac”等)谢谢!
更新: @Codebender 我的代码现在看起来像这样。
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("open -a /Applications/Utilities/Terminal.app");
new PrintStream(pr.getOutputStream).println("ls");
我收到错误“找不到符号,符号:变量 getOutputStream,位置:进程类型的变量 pr”和 getOutputStream 下的红线。有什么想法吗?
@Codebender 那应该是这样吗?
new PrintStream(pr.getOutputStream{println("ls")});
【问题讨论】:
-
@Codebender 我没有。你能用代码告诉我这会是什么样子吗?我尝试将这行代码复制并粘贴到我的脚本中,但我不认为应该这样执行。我对java的这方面相当陌生。谢谢:)
-
@Codebender 我在帖子结束后添加了一条评论作为回应。
-
所以应该是这样的吧? (见帖子更新)
-
我已经在答案中更新了它。
标签: java macos user-interface netbeans terminal