This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Aug 31, 2012 2:01 PM by sabre150 RSS

Not able to invoke a process for cmd using Runtime.getRuntime()

958704 Newbie
Currently Being Moderated
Trying to invoke an external thread using Runtime.getRuntime().exec(String command) where command is cmd path.

String command="C:\\Windows\\System32\\cmd.exe;
Runtime.getRuntime().exec(command);

This is supposed to invoke cmd in a separate thread but its not working. In place of cmd, if I use notepad.exe or wmplayer.exe, it works.

Tried the following code in order to capture the invoked cmd thread in a process during runtime and destroy it. But to no avail. The line of code with exec is not invoking the cmd or a batch file.

Process proc= Runtime.getRuntime().exec(command);
proc.destroy();

Any help is greatly appreciated.
  • 1. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    linkin Newbie
    Currently Being Moderated
    try this
    Runtime rt = Runtime.getRuntime(); 
    rt.exec(new String[]{"cmd.exe","/c"}); 
  • 2. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    sabre150 Expert
    Currently Being Moderated
    955701 wrote:
    Trying to invoke an external thread using Runtime.getRuntime().exec(String command) where command is cmd path.

    String command="C:\\Windows\\System32\\cmd.exe;
    Runtime.getRuntime().exec(command);

    This is supposed to invoke cmd in a separate thread but its not working. In place of cmd, if I use notepad.exe or wmplayer.exe, it works.

    Tried the following code in order to capture the invoked cmd thread in a process during runtime and destroy it. But to no avail. The line of code with exec is not invoking the cmd or a batch file.

    Process proc= Runtime.getRuntime().exec(command);
    proc.destroy();

    Any help is greatly appreciated.
    The destroy() will probably kill off the process before it has chance to execute.

    You should wait for the process to complete using
    int exitCode = proc.waitFor();
    You should also read the 3 sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and implement ALL the recommendations. You might get away with not implementing the recommendations but at sometime traps will jump up and bite you.
  • 3. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    sabre150 Expert
    Currently Being Moderated
    linkin wrote:
    try this
    Runtime rt = Runtime.getRuntime(); 
    rt.exec(new String[]{"cmd.exe","/c"}); 
    With what aim? From the cmd.exe help the "/C says
    /C      Carries out the command specified by string and then terminates
    which means that a third argument is needed - the command to be execute by cmd.exe .
  • 5. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    958704 Newbie
    Currently Being Moderated
    Thank you for replying. I have tried what you have mentioned :

    Runtime rt = Runtime.getRuntime();
    rt.exec(new String[]{"cmd.exe","/c"});

    But it didn't invoke the cmd window. When any of windows based exe files can be invoked using this code, not sure why cmd is not opening. It just runs the program without giving any error. Guess it may be related to my administrative access to open cmd ? Is there any option we can invoke the cmd process as administrator ? This can be done manually by Start--cmd in search--right click on cmd.exe --Run as administrator.

    I need to get this working asap as I have to invoke some batch files from my java program. Since cmd itself is not able to invoke, so are the batch files. :(
    I need to have a look at http://www.javaworld.com/jw-12-2000/jw-1229-traps.html though.

    Thanks
    Ravi.
  • 6. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    sabre150 Expert
    Currently Being Moderated
    955701 wrote:
    Thank you for replying. I have tried what you have mentioned :

    Runtime rt = Runtime.getRuntime();
    rt.exec(new String[]{"cmd.exe","/c"});

    But it didn't invoke the cmd window.
    You have not supplied the third argument !!!!!!!!!!!!!!!!!!! The command to execute.

    Note - whether or not you need to invoke cmd.exe depends on exactly what you are trying to run but you don't say what this is!
  • 7. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    linkin Newbie
    Currently Being Moderated
    To 955701
    Runtime rt = Runtime.getRuntime(); 
    rt.exec(new String[]{"cmd.exe","/c","start"}); 
    You have pass the command what you want to run on cmd as third param.
    You should also check what sabre150 told to add waitFor() .



    Edited by: linkin on Aug 29, 2012 6:22 PM

    Edited by: linkin on Aug 29, 2012 6:23 PM
  • 8. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    rp0428 Guru
    Currently Being Moderated
    >
    Trying to invoke an external thread using Runtime.getRuntime().exec(String command) where command is cmd path.
    >
    See if the example of running a batch file at the end of this blog does what you want
    http://forums.devshed.com/java-help-9/how-to-run-a-batch-file-from-a-java-program-565750.html
    1.import java.io.*;
    2. 
    3.public class Foo {
    4. 
    5.    public static void main(String[] args) throws IOException {
    6.        java.lang.ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "test.bat");
    7.        java.lang.Process p = pb.start();
    8. 
    9.        String line;
    10.        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    11.        while ((line = r.readLine()) != null) {
    12.            System.out.println(line);
    13.        }
    14.        r.close();
    15.    }
    16.} 
  • 9. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    sabre150 Expert
    Currently Being Moderated
    rp0428 wrote:
    >
    Trying to invoke an external thread using Runtime.getRuntime().exec(String command) where command is cmd path.
    >
    See if the example of running a batch file at the end of this blog does what you want
    http://forums.devshed.com/java-help-9/how-to-run-a-batch-file-from-a-java-program-565750.html
    1.import java.io.*;
    2. 
    3.public class Foo {
    4. 
    5.    public static void main(String[] args) throws IOException {
    6.        java.lang.ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "test.bat");
    7.        java.lang.Process p = pb.start();
    8. 
    9.        String line;
    10.        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
    11.        while ((line = r.readLine()) != null) {
    12.            System.out.println(line);
    13.        }
    14.        r.close();
    15.    }
    16.} 
    Except that that code can deadlock if the process (not Process) stderr buffer fills.
  • 10. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    rp0428 Guru
    Currently Being Moderated
    >
    Except that that code can deadlock if the process (not Process) stderr buffer fills.
    >
    Well as with all sample code it is meant to illustrate the basics of how to do what OP wants to do. I assumed it is understood that sample code does not include complete error and exception handling.
  • 11. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    sabre150 Expert
    Currently Being Moderated
    rp0428 wrote:
    >
    Except that that code can deadlock if the process (not Process) stderr buffer fills.
    >
    Well as with all sample code it is meant to illustrate the basics of how to do what OP wants to do. I assumed it is understood that sample code does not include complete error and exception handling.
    It is my experience that code samples published in these forums and many other are used verbatim without the user thinking about what else is required. In this case the traps as outlined in the article are serious which is why I always reference the ancient but still valid traps article. While I agree that the use of ProcessBuilder is superior to the use of Runtime.exec(), since your code spells out that 'stdout' has to be handled your code, by omission, gives the impression that 'stderr' does not need to be handled. Whether one uses Runtime.exec() or ProcessBuilder to create a Process the traps article still applies and ALL the recommendations need to be implemented.
  • 12. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    958704 Newbie
    Currently Being Moderated
    Hi All,

    Basically I was trying to execute a batch file from command prompt. Following piece of code worked. :

    Runtime rt = Runtime.getRuntime();
    Process proc=rt.exec(new String[]{"cmd.exe","/c", "C:\\Users\\u0160720\\Desktop\\test1.bat"});
    int exitCode=proc.waitFor();

    Where test1.bat does its specific job.

    Thanks!
  • 13. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    sabre150 Expert
    Currently Being Moderated
    955701 wrote:
    Hi All,

    Basically I was trying to execute a batch file from command prompt. Following piece of code worked. :

    Runtime rt = Runtime.getRuntime();
    Process proc=rt.exec(new String[]{"cmd.exe","/c", "C:\\Users\\u0160720\\Desktop\\test1.bat"});
    int exitCode=proc.waitFor();

    Where test1.bat does its specific job.

    Thanks!
    Jesus!!!!!!!!! I flippin give up; what is the point of coming to these forums for advice when you just ignore it?
  • 14. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
    DrClap Expert
    Currently Being Moderated
    sabre150 wrote:
    Jesus!!!!!!!!! I flippin give up; what is the point of coming to these forums for advice when you just ignore it?
    It's just like driving a car... most people think they are good drivers when in fact they have just been lucky so far.
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points