0

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
3
Contributors
10
Replies
14
Views
6 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • 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 [url]http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/[/url] (the relevant bit is right at the bottom) I recommend option (2) Read More

0

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

0

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
0

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.

0

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)

0

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

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 by mike_2000_17: Fixed formatting

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.