views:

41

answers:

1

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

+2  A: 

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.

Codemwnci