[英]Open Command line + inputs for GUI
I am trying to write a java GUI in netbeans for executing a program on the command line, and currently have this piece of code assigned to a button 我正在尝试在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();
}
}
This opens the terminal, however I would like to know how I should go about inputting commands into the terminal while still just pressing the button (ex: "ls", "cd", "javac" etc) Thanks! 这将打开终端,但是我想知道如何在仍然只按按钮的同时向终端输入命令(例如:“ ls”,“ cd”,“ javac”等)。谢谢!
UPDATE: @Codebender My code now looks like this. 更新:@Codebender我的代码现在看起来像这样。
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("open -a /Applications/Utilities/Terminal.app");
new PrintStream(pr.getOutputStream).println("ls");
I am getting the error "cannot find symbol, symbol: variable getOutputStream, location: variable pr of type process" and a red line under getOutputStream. 我收到错误消息“找不到符号,符号:类型为process的变量getOutputStream,位置:变量pr”,并且在getOutputStream下出现一条红线。 Any ideas?
有任何想法吗?
@Codebender So should it be like this? @Codebender那么应该是这样吗?
new PrintStream(pr.getOutputStream{println("ls")});
Use can use outputStream to write to the terminal. 使用可以使用outputStream写入终端。 Wrap it up with a printstream to make things easier.
用printstream打包起来使事情变得更容易。
Process pr = rt.exec("open -a /Applications/Utilities/Terminal.app");
PrintStream ps = new PrintStream(pr.getOutputStream());
ps.println("ls" + System.lineSeparator());
// Follow with the reading of output from terminal.
If your Terminal.app
is the default linux terminal, instead of opening a new one you can try, 如果您的
Terminal.app
是默认的Linux终端,则可以尝试打开一个新的终端,而不用打开一个新终端,
Process pr = rt.exec("ls");
// Follow with the reading of output.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.