从应用程序启动终端

发布于 2024-09-27 15:16:58 字数 1278 浏览 1 评论 0原文

我使用 Netbeans 6.9 创建了一个应用程序。在应用程序中,我希望当用户单击运行按钮时,终端(命令提示符)应该打开,并且终端上应该出现一些文本。文字实际上是一个命令。我希望该命令应该在终端上执行。任何人都可以帮助我吗? 我编写了以下代码....

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();  
        }  
    }  
}

但它给出了以下错误...

cmd.exe/start cmd
java.io.IOException:无法运行程序“cmd.exe/start”:CreateProcess error=2,
系统找不到指定的文件
在 java.lang.ProcessBuilder.start(ProcessBuilder.java:1018)
在 java.lang.Runtime.exec(Runtime.java:610)
在 java.lang.Runtime.exec(Runtime.java:448)
在 java.lang.Runtime.exec(Runtime.java:345)
在 test.main(test.java:6)
引起原因:java.io.IOException: CreateProcess error=2, 系统找不到th
指定的 e 文件
在 java.lang.ProcessImpl.create(本机方法)
在 java.lang.ProcessImpl.(ProcessImpl.java:155)
在 java.lang.ProcessImpl.start(ProcessImpl.java:99)
在 java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)
... 4 more

谁能告诉我出了什么问题吗?

-提前致谢

I have created an application using 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
java.io.IOException: Cannot run program "cmd.exe/start": CreateProcess error=2,
The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1018)
at java.lang.Runtime.exec(Runtime.java:610)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at test.main(test.java:6)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:155)
at java.lang.ProcessImpl.start(ProcessImpl.java:99)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)
... 4 more

Can anyone tell me whats the problem??

-Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2024-10-04 15:16:58

这是一个关于 Java 运行时和进程的非常好的教程,它涵盖了您想要做的所有要点。

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

简单来说,就是想用Runtime打开命令窗口,并读取Process并写入该进程的输出流。

Here is a really good tutorial on Runtime and Process in Java, which covers off all the points that you are looking to do.

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.

骄兵必败 2024-10-04 15:16:58

错误出现在您的命令中.. "cmd.exe/start cmd"

Process prr = rt.exec("cmd /c "+i);
在这种情况下,您要执行的命令位于 (String i)

The error is in your command.. "cmd.exe/start cmd"

Process prr = rt.exec("cmd /c "+i);
in this case the command you want to execute is in (String i)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文