This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I've been using Runtime to execute basic commands on my Mac, and also through a local network on my moms PC. Worked fine until a little bit ago. now I just get errors on the pc, but not on my mac. does this have to do with 1.4.1 to 1.4.2 update? that would be weird wouldnt it? on the pc i get the following error: C:\Dokumente und Einstellungen\Susan\Desktop>java something java.io.IOException: CreateProcess: dir error=2 at java.lang.Win32Process.create(Native Method) at java.lang.Win32Process.<init>(Unknown Source) at java.lang.Runtime.execInternal(Native Method) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at something.main(something.java:36) here is the code.
This might get tricky ... I remember dir is one of those commands that is not a program so it can't be launched as a program. You can run command.com (cmd.com on some systems?) and then send "dir" to its standard input. Oh, and then send "exit" to make command.com end. You can try all that in a DOS window. You may be able to launch a bat file that does dir for you. Much simpler!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Your code tries to read from streams when the process associated with them has been completed. Might be the case.
Joined: Jan 29, 2003
I wondered about those stream readers. Each just reads until end on the main thread. I'm not sure that will fly, but it's worth trying. I did a runtime() thingie once and made separate threads to read the stdout and errout. They ALWAYS finished well AFTER the waitfor() returned. I guessed that output must get buffered up somewhere and my asynchronous reads were relatively slow compared to the process's writes.
I use rt.exec("cmd.exe"),although windows create "CMD.EXe" process, But I can't see it. I think that "cmd.exe" can't create a really process. Who tell me how to do it!
Joined: Mar 11, 2004
The Runtime.exec methods create a native process and return an instance of a subclass of Process that can be used to control the process and obtain information about it. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process. The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock. The subprocess is not killed when there are no more references to the Process object, but rather the subprocess continues executing asynchronously. There is no requirement that a process represented by a Process object execute asynchronously or concurrently with respect to the Java process that owns the Process object.