1

I've been having problems reading output of windows command line from Java, i'm using Runtime.getRuntime().exec()

I simplified my test case: I have a file called お読みください.txt, and i execute the following command cmd /c dir C:/PATH

Note: The actual command is tasklist, but it's the same result as long as i use Runtime.getRuntime().exec()

String[] cmd = new String[]{ "cmd", "/c", "dir" };
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = 
    new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
String s, result = "";
while ((s = stdInput.readLine()) != null) {
    if (!s.isEmpty()) {
        result += s + "\n";
    }
}
System.out.println(result);

I just get ���ǂ݂�������.txt

I tried with no charset, default, and the other ones; after testing all charsets, i got the one i was looking for: Shift_JIS

And that must be because i have set Language for non-Unicode applications as Japanese. systeminfo.exe says ja;Japanese for Regional Config.

I can simply use Shift_JIS to read, but it will only work in my computer. What about other system configurations?

The question is, how can i get the correct charset to read Windows Console output?

dpaladin
  • 67
  • 1
  • 11
  • Reason 834 not to fork OS specific commands. Why don't you use `File("mydir").listFiles()`? – Alastair McCormack Feb 04 '16 at 19:32
  • @AlastairMcCormack Sorry, i tried to simplify my example. The actual command is tasklist, and this is only for Windows. The problem is Java doesn't like other encodings. – dpaladin Feb 06 '16 at 07:54

1 Answers1

1

Base on the answer of What encoding/code page is cmd.exe using?

You can execute cmd /k chcp && pause && exit to get current code page. Using Code Page Identifiers to find the mapping Java encoding name.

tresf
  • 7,103
  • 6
  • 40
  • 101
Beck Yang
  • 3,004
  • 2
  • 21
  • 26
  • won't you have problems reading the output without knowing the charset before? – DGoiko Jan 18 '20 at 23:10
  • The output of `chcp` is a 7bit ASCII string, ex: `Active code page: 437`. There is no encoding problem to parse it. – Beck Yang Jan 24 '20 at 11:17
  • 2
    In a spnaish windows machine it outputs "Página de códigos activa: "... But I guess you could execute a regex to extract it like ".*:\s*\(d+)" – DGoiko Jan 24 '20 at 18:54