Computing.Net > Forums > Programming > java cmd.exe

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

java cmd.exe

Reply to Message Icon

Name: buta
Date: September 25, 2006 at 18:48:51 Pacific
OS: windows
CPU/Ram: 1g
Product: dell
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: buta
Date: September 26, 2006 at 01:00:13 Pacific
Reply:

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 is

free tune up http://www.pcpitstop.com


0

Response Number 2
Name: Shr0Om
Date: September 26, 2006 at 01:36:50 Pacific
Reply:

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?


0

Response Number 3
Name: buta
Date: September 26, 2006 at 08:46:01 Pacific
Reply:

No, I can't. Must be in c:\

free tune up http://www.pcpitstop.com


0

Response Number 4
Name: Mechanix2Go
Date: September 26, 2006 at 08:51:35 Pacific
Reply:

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



0

Response Number 5
Name: Guy
Date: September 26, 2006 at 19:07:33 Pacific
Reply:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: java cmd.exe

How to open cmd.exe in VB6 www.computing.net/answers/programming/how-to-open-cmdexe-in-vb6/7943.html

java to exe www.computing.net/answers/programming/java-to-exe/2246.html

need MD5 hash for XP CMD.EXE & PING www.computing.net/answers/programming/need-md5-hash-for-xp-cmdexe-ping/16712.html