954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem in executing cmd commands with java

some commands working properly(notepad ,control(control panel))...


but how to execute commands "dir" like that

my code is ...

public class test1{

public static void main(String args[])throws Exception{





Runtime r= Runtime.getRuntime();

Process p1=r.exec("notepad");//working fine
Process p2=r.exec("control");//working fine
Process p3=r.exec("test1.java");//cannot run
Process p4=r.exec("dir");// cannot run

}

}



o/p:

F:\studies\java\test>java test1

Exception in thread "main" java.io.IOException: Cannot run program "test1.java":
 CreateProcess error=193, %1 is not a valid Win32 application
        at java.lang.ProcessBuilder.start(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at test1.main(test1.java:18)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32
 application
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        ... 5 more
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

things like dir are commands processed by cmd.exe - the windows command processor.
You can explicitly run one of cmd's commands with the /C option, eg
cmd.exe /C dir
and you can use processbuilder to create a suitable process as in
Process p = new ProcessBuilder("cmd.exe", "/C", "dir")

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

To open the appropriate registered application and open a file eg test1.java you can
(1) use processbuilder and explicitly specify the application or
(2) use Java 1.6's desktop integration to do it all for you
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/
(the relevant bit is right at the bottom)

I recommend option (2)

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
things like dir are commands processed by cmd.exe - the windows command processor. You can explicitly run one of cmd's commands with the /C option, eg cmd.exe /C dir and you can use processbuilder to create a suitable process as in Process p = new ProcessBuilder("cmd.exe", "/C", "dir")

am try to use processBuilder it specify compilation error(incompatible types)..

code:

import java.lang.Runtime;
import java.lang.*;
public class test1{
public static void main(String args[])throws Exception{
//Process process = Runtime.getRuntime().exec("notepad.exe");

Runtime r= Runtime.getRuntime();

Process p2=r.exec("control");//working fine

Process p = new ProcessBuilder("cmd.exe", "/C", "dir");
}
}

error:

F:\studies\java\test>javac test1.java
test1.java:21: incompatible types
found   : java.lang.ProcessBuilder
required: java.lang.Process
Process p = new ProcessBuilder("cmd.exe", "/C", "dir");
            ^
1 error
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 
incompatible types found : java.lang.ProcessBuilder required: java.lang.Process


The compiler does not think that a Process object is compatible with a ProcessBuilder object. Why do you want to assign a ProcessBuilder object to a Process variable?
You should change your code to use a ProcessBuilder variable.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
The compiler does not think that a Process object is compatible with a ProcessBuilder object. Why do you want to assign a ProcessBuilder object to a Process variable? You should change your code to use a ProcessBuilder variable.
ProcessBuilder p = new ProcessBuilder("cmd.exe", "/C", "dir");


am tried , but it can't work (that means it does n't show dir list in console window)

sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

am tried another also it can't work

ProcessBuilder l = new ProcessBuilder();
l.command("dir");
 Process p = launcher.start();

run time error:

Exception in thread "main" java.io.IOException: Cannot run program "dir": Create
Process error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(Unknown Source)
        at test1.main(test1.java:27)
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.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        ... 2 more
sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 
ProcessBuilder p = new ProcessBuilder("cmd.exe", "/C", "dir");

am tried , but it can't work (that means it does n't show dir list in console window)

It's (probably) working, but the output from a ProcessBuilder execution goes to a stream where you can read and process it. The following sample shows how to get that input - in this case it just copies it to the console

try {
         Process p = new ProcessBuilder("cmd.exe", "/C", "dir")
               .redirectErrorStream(true).start();
         InputStream is = p.getInputStream();
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String in;
         while ((in = br.readLine()) != null) {
            System.out.println(in);
         }
      }catch(Exception e) {
         e.printStackTrace();
      }
JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

am tried another also it can't work

ProcessBuilder l = new ProcessBuilder();
l.command("dir");
 Process p = launcher.start();

...[/CODE]

I explained earlier why this will not work.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

It's (probably) working, but the output from a ProcessBuilder execution goes to a stream where you can read and process it. The following sample shows how to get that input - in this case it just copies it to the console

try {
         Process p = new ProcessBuilder("cmd.exe", "/C", "dir")
               .redirectErrorStream(true).start();
         InputStream is = p.getInputStream();
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String in;
         while ((in = br.readLine()) != null) {
            System.out.println(in);
         }
      }catch(Exception e) {
         e.printStackTrace();
      }


ya working fine....

redirectErrorStream(true).start();// i cant understand what is the function of redirectErrorStream

sathya88
Junior Poster in Training
55 posts since Sep 2010
Reputation Points: 10
Solved Threads: 3
 

Sends any errors to the same stream as the normal output, saves you having to look in 2 places for results.

JamesCherrill
Posting Genius
Moderator
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: