Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I've been trying to execute cmd.exe from java, but having not been able to solve the problem.
In this example, I happen to use gpg, but any commands will show the same symptom. When I run the code, it just hangs and sits forever. However I can execute the command from the console without any problem.
I am wondering if this is a common problem in java. I don't think I am doing something wrong here.import java.io.*;
import java.util.*;
import java.lang.Runtime.*;public class Test
{
public static void main(String[] args)
{
String[] cmd = new String[2];
cmd[0] = "cmd.exe";
cmd[1] = "gpg --passphrase-fd 0 < passphrase.txt -o test.txt -d test.pgp";
try
{
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}free tune up http://www.pcpitstop.com

I figured that I have to execute cmd.exe in the root directory as follows. This is pretty useless if the source is not in the root directory. This doesn't make any sense!
String[] cmd = new String[2];
cmd[0] = "cmd.exe";
cmd[1] = "/C";
cmd[1] = "dir"; //whatever the command isfree tune up http://www.pcpitstop.com

Strange.. I dont know much java but, in eg. Perl you can launch cmd commands no matter where the source is located. Btw, it shouldnt be necesary to launch CMD before executing any other commands. Cant you just execute `dir` or any other commands?

Sounds like something is hosed.
What does your path look like?
And where is CMD?
=====================================
If at first you don't succeed, you're about average.M2

It hangs because you are not consuming the stderr (and stdout) streams produced by the new process.
You absolutely *must* do this with stderr, because it is not buffered, and it is good practice with stdout.
See this for details and examples:
http://www.javaworld.com/javaworld/...
Guy

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |