Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打开命令行+;GUI的输入_Java_Macos_User Interface_Netbeans_Terminal - Fatal编程技术网

Java 打开命令行+;GUI的输入

Java 打开命令行+;GUI的输入,java,macos,user-interface,netbeans,terminal,Java,Macos,User Interface,Netbeans,Terminal,我正在尝试用netbeans编写一个javagui,用于在命令行上执行一个程序,并且目前将这段代码分配给一个按钮 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try { Runtime rt = Runtime.getRuntime(); Process

我正在尝试用netbeans编写一个javagui,用于在命令行上执行一个程序,并且目前将这段代码分配给一个按钮

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”等)的同时将命令输入终端,谢谢

更新: @我的代码现在看起来像这样

Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec("open -a /Applications/Utilities/Terminal.app");
            new PrintStream(pr.getOutputStream).println("ls");
我收到错误“找不到符号,符号:变量getOutputStream,位置:类型为process的变量pr”,getOutputStream下有一条红线。有什么想法吗

@那么应该是这样吗

new PrintStream(pr.getOutputStream{println("ls")});

Use可以使用outputStream写入终端。用打印流将其包装起来,使事情变得更简单

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.
如果您的
Terminal.app
是默认的linux终端,您可以尝试打开一个新的

Process pr = rt.exec("ls");
// Follow with the reading of output.

@我没有。你能用代码告诉我这是什么样子吗?我试着将这行代码复制并粘贴到我的脚本中,但我不认为应该这样执行。我对java的这一方面相当陌生。谢谢:)@Codebender我在帖子末尾加了一条评论作为回应。那么应该是这样吗?(请参阅帖子中的更新)我已在回答中对其进行了更新。这没有起任何作用:(.它只是启动了我的终端,然后像“退出时出现错误代码0”之前一样完成了(基本上完成了程序)。我还能提供更多信息吗?我觉得我可能做错了什么。谢谢你的帮助!@Shambaree检查我的替代解决方案是否对你有效。没有。:(我只想在终端中输入一个命令,并在它打开后执行它。顺便说一下,我在OSX上的Netbeans中