0

I am running the following using Apache exec lib.

wmic LOGICALDISK GET Name,ProviderName /FORMAT

This command returns a listing of all the mapped drives and their mapping. When I run it from the command line, it works great. When I run it from within java, it returns the first 2 drives and 1 of the drives listed in the middle.

I've piped the stream to stdout, used stream gobbler, etc. I have several other commands I run that work fine and I read the streams without problem. I'm stumped. Any ideas? Encoding possibly? I've never had this problem before.

Oh, I've also run with ProcessBuilder, Runtime.exec, and DefaultExecutor. Same results throughout.

Thanks.

Marius Waldal
  • 9,537
  • 4
  • 30
  • 44
  • have you tried running "wmic LOGICALDISK GET Name,ProviderName /FORMAT > file.txt" and comparing that with your results? – radai Mar 20 '14 at 18:43
  • Its really weird.  I have the mappings.cmd file in my jar and just dump it as a tmp file so I can run it.  I am using Netbeans and it will let me execute the file internally.  Netbeans even gets the same output. Output run from command Line C: D: G:  \\SomeMachine\somedir F:  \\SomeOtherMachine\otherdir Y:  \\DomainController\domain Z:  \\DBServer\dbdir The output I get when executing it from the java runtime C: D: F:  \\SomeOtherMachine – Just Lick It Mar 20 '14 at 20:33
  • I did the command rediredt. If i run from the command line, it's perfect. If i run from java, the output is still messed up inside the file. – Just Lick It Mar 20 '14 at 20:35
  • are you certain youre reading from the process' stout as well as stderr ? – radai Mar 21 '14 at 07:50
  • Ya. I've done it a million ways. The strange thing is Netbeans gets the same output if I run the command as a batch file from within the ide. I've set the /OUTPUT and /APPEND tags to files, stdout, etc. I might just have to find a way to run the script on startup using a startup batch and write the output then. – Just Lick It Mar 21 '14 at 14:58

1 Answers1

0

stdbuf -o0 wmic LOGICALDISK GET Name,ProviderName /FORMAT | cat

download stdbuf from: http://www.inreto.de/ffp/0.7/arm/packages/coreutils-8.14-arm-1.txz

or maybe:

public static String execCmd(String cmd) throws java.io.IOException {
    Process proc = Runtime.getRuntime().exec(cmd);
    java.io.InputStream is = proc.getInputStream();
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    String val = "";
    if (s.hasNext()) {
        val = s.next();
    }
    else {
        val = "";
    }
    return val;
}

from:https://stackoverflow.com/a/20624914/264181

diyism
  • 12,477
  • 5
  • 46
  • 46