Actually, the encoding used by tasklist is always different from the system default.
On the other hand, it is safe to use the default value while output is limited to ASCII . Typically, executables only have ASCII characters in their names.
So, in order to get the correct lines, you need to convert (ANSI) the Windows codepage to the OEM code page and pass the latter as a charset to the InputStreamReader .
There seems to be no complete comparison between these encodings. You can use the following mapping:
Map<String, String> ansi2oem = new HashMap<String, String>(); ansi2oem.put("windows-1250", "IBM852"); ansi2oem.put("windows-1251", "IBM866"); ansi2oem.put("windows-1252", "IBM850"); ansi2oem.put("windows-1253", "IBM869"); Charset charset = Charset.defaultCharset(); String streamCharset = ansi2oem.get(charset.name()); if (streamCharset) { streamCharset = charset.name(); } InputStreamReader isr = new InputStreamReader(p.getInputStream(), streamCharset);
This approach worked for me with windows-1251 and IBM866 .
To get the current OEM encoding used by Windows, you can use the GetOEMCP function. The return value depends on the Language parameter for non-Unicode programs on the Administrative tab in the Region and language panel. A reboot is required to make changes.
There are two types of encodings on Windows: ANSI and OEM .
The first is used by non-Unicode applications running in GUI mode. The latter is used by console applications. Console applications cannot display characters that cannot be represented in the current OEM encoding.
Since tasklist is an application in console mode, its output is always in the current OEM encoding.
For English steam systems, usually Windows-1252 and CP850 .
As in Russia, my system has the following encodings: Windows-1251 and CP866 .
If I write the output of tasklist to a file, the file will not be able to correctly display Cyrillic characters:
I get ะัา instead of (Hi!) When browsing in Notepad.
And ยตTorrent displayed as Torrent .
You cannot change the encoding used by tasklist .
However, it is possible to change the output encoding of cmd . If you give him /u , it will output everything in UTF-16 encoding.
cmd /c echo Hi>echo.txt
The size of echo.txt is 4 bytes: two bytes for Hi and two bytes for a new line ( \r and \n ).
cmd /u /c echo Hi>echo.txt
Now the size of echo.txt is 8 bytes: each character is represented by two bytes.