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

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")

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

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.

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)

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

am tried another also it can't work

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

I explained earlier why this will not work.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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

This article has been dead for over six months. Start a new discussion instead.