简体   繁体   English

从应用程序启动终端

[英]launching terminal from application

I have created an application using Netbeans 6.9. 我已经使用Netbeans 6.9创建了一个应用程序。 In the application I want that when the user clicks on the run button then the terminal(Command Prompt) should open and some text should appear on the terminal. 在应用程序中,我希望当用户单击运行按钮时,终端(命令提示符)应打开,并且终端上应显示一些文本。 The text is actually a command. 文本实际上是命令。 I want that the command should be executed on the terminal. 我希望该命令应在终端上执行。 Can anyone please help me. 谁能帮帮我吗。 I've written the following code.... 我写了下面的代码。

class test extends Exception{  
    public static void main(String arg[]) {  
        String command = "cmd.exe/start cmd";  
        System.out.println(command);  
        try {  
            Process child = Runtime.getRuntime().exec(command);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}

But its giving the following error... 但是它给出了以下错误...

cmd.exe/start cmd cmd.exe /启动cmd
java.io.IOException: Cannot run program "cmd.exe/start": CreateProcess error=2, java.io.IOException:无法运行程序“ cmd.exe / start”:CreateProcess错误= 2,
The system cannot find the file specified 该系统找不到指定的文件
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1018) 在java.lang.ProcessBuilder.start(ProcessBuilder.java:1018)
at java.lang.Runtime.exec(Runtime.java:610) 在java.lang.Runtime.exec(Runtime.java:610)
at java.lang.Runtime.exec(Runtime.java:448) 在java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345) 在java.lang.Runtime.exec(Runtime.java:345)
at test.main(test.java:6) 在test.main(test.java:6)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th 由以下原因引起:java.io.IOException:CreateProcess错误= 2,系统找不到
e file specified 指定的e文件
at java.lang.ProcessImpl.create(Native Method) 在java.lang.ProcessImpl.create(本机方法)
at java.lang.ProcessImpl.(ProcessImpl.java:155) 在java.lang.ProcessImpl。(ProcessImpl.java:155)
at java.lang.ProcessImpl.start(ProcessImpl.java:99) 在java.lang.ProcessImpl.start(ProcessImpl.java:99)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010) 在java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)
... 4 more ...另外4个

Can anyone tell me whats the problem?? 谁能告诉我是什么问题?

-Thanks in advance -提前致谢

Here is a really good tutorial on Runtime and Process in Java, which covers off all the points that you are looking to do. 这是一个有关Java运行时和流程的非常好的教程,它涵盖了您要执行的所有操作。

http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html

Simply, you want to use Runtime to open the command window, and the Process to read and write to the output stream of that process. 简而言之,您只想使用运行时来打开命令窗口,并使用Process来读写该进程的输出流。

The error is in your command.. "cmd.exe/start cmd" 该错误在您的命令中。.“ cmd.exe / start cmd”

Process prr = rt.exec("cmd /c "+i); 进程prr = rt.exec(“ cmd / c” + i); in this case the command you want to execute is in (String i) 在这种情况下,您要执行的命令在(字符串i)中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM