This content has been marked as final.
Show 15 replies
-
1. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
linkin Aug 29, 2012 10:37 AM (in response to 958704)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 Aug 29, 2012 11:18 AM (in response to 958704)955701 wrote:
The destroy() will probably kill off the process before it has chance to execute.
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.
You should wait for the process to complete using
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.int exitCode = proc.waitFor();
-
3. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
sabre150 Aug 29, 2012 11:16 AM (in response to linkin)linkin wrote:
With what aim? From the cmd.exe help the "/C says
try this
Runtime rt = Runtime.getRuntime(); rt.exec(new String[]{"cmd.exe","/c"});
which means that a third argument is needed - the command to be execute by cmd.exe ./C Carries out the command specified by string and then terminates
-
4. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
linkin Aug 29, 2012 12:08 PM (in response to sabre150)yes. -
5. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
958704 Aug 29, 2012 12:21 PM (in response to linkin)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 Aug 29, 2012 12:26 PM (in response to 958704)955701 wrote:
You have not supplied the third argument !!!!!!!!!!!!!!!!!!! The command to execute.
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.
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 Aug 29, 2012 12:53 PM (in response to sabre150)To 955701
You have pass the command what you want to run on cmd as third param.Runtime rt = Runtime.getRuntime(); rt.exec(new String[]{"cmd.exe","/c","start"});
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 Aug 29, 2012 3:23 PM (in response to 958704)>
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.html1.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 Aug 29, 2012 3:28 PM (in response to rp0428)rp0428 wrote:
Except that that code can deadlock if the process (not Process) stderr buffer fills.
>
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.html1.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.}
-
10. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
rp0428 Aug 29, 2012 6:54 PM (in response to sabre150)>
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 Aug 29, 2012 8:06 PM (in response to rp0428)rp0428 wrote:
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.
>
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. -
12. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
958704 Aug 31, 2012 6:37 AM (in response to sabre150)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 Aug 31, 2012 7:03 AM (in response to 958704)955701 wrote:
Jesus!!!!!!!!! I flippin give up; what is the point of coming to these forums for advice when you just ignore it?
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! -
14. Re: Not able to invoke a process for cmd using Runtime.getRuntime()
DrClap Aug 31, 2012 5:25 PM (in response to sabre150)sabre150 wrote:
It's just like driving a car... most people think they are good drivers when in fact they have just been lucky so far.
Jesus!!!!!!!!! I flippin give up; what is the point of coming to these forums for advice when you just ignore it?