Get list of processes on Windows in a charset-safe way

后端 未结 4 1884
别那么骄傲
别那么骄傲 2020-12-01 19:27

This post gives a solution to retrieve the list of running processes under Windows. In essence it does:

String cmd = System.getenv(\"windir\") + \"\\\\system         


        
4条回答
  •  情话喂你
    2020-12-01 19:54

    There is a much better way to check the running processes, or even to run OS command through java: Process and ProcessBuilder.

    As for the Charset, you can always inquire the OS about the supported charsets, and obtain an Encoder or Decoder according to your needs.

    [Edit] Let's break it down; there's no way of knowing in which encoding the bytes of a given String are, so your only choice is to get those bytes, shift the ordering as necessary (if you're ever in such an environment where a process can give you an array of bytes in different ordering, use ByteBuffer to deal with that), and use the multiple CharsetDecoders supported to decode the bytes to reasonable output.

    It is overkill and requires you to estimate that a given output could be in UTF-8, UTF-16 or any other encoding. But at least you can decode the given output using one of the possible Charsets, and then try to use the processed output for your needs.

    Since we're talking about a process run by the same OS in which the JVM itself is running, it is quite possible that your output will be in one of the Charset encodings returned by the availableCharsets() method.

提交回复
热议问题