Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:37:46+00:00 2026-06-14T17:37:46+00:00

This post gives a solution to retrieve the list of running processes under Windows.

  • 0

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

String cmd = System.getenv("windir") + "\\system32\\" + "tasklist.exe";
Process p = Runtime.getRuntime().exec(cmd);
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader input = new BufferedReader(isr);

then reads the input.

It looks and works great but I was wondering if there is a possibility that the charset used by tasklist might not be the default charset and that this call could fail?

For example this other question about a different executable shows that it could cause some issues.

If that is the case, is there a way to determine what the appropriate charset would be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:37:47+00:00Added an answer on June 14, 2026 at 5:37 pm

    Can break this into 2 parts:

    1. The windows part
      From java you’re executing a Windows command – externally to the jvm in “Windows land”. When java Runtime class executes a windows command, it uses the DLL for consoles & so appears to windows as if the command is running in a console
      Q: When I run C:\windows\system32\tasklist.exe in a console, what is the character encoding (“code page” in windows terminology) of the result?

      • windows “chcp” command with no argument gives the active code page number for the console (e.g. 850 for Multilingual-Latin-1, 1252 for Latin-1). See Windows Microsoft Code Pages, Windows OEM Code Pages, Windows ISO Code Pages
        The default system code page is originally setup according to your system locale (type systeminfo to see this or Control Panel-> Region and Language).
      • the windows OS/.NET function getACP() also gives this info
    2. The java part:
      How do I decode a java byte stream from the windows code page of “x” (e.g. 850 or 1252)?

      • the full mapping between windows code page numbers and equivalent java charset names can be derived from here – Code Page Identifiers (Windows)
      • However, in practice one of the following prefixes can be added to achieve the mapping:
        “” (none) for ISO, “IBM” or “x-IBM” for OEM, “windows-” OR “x-windows-” for Microsoft/Windows.
        E.g. ISO-8859-1 or IBM850 or windows-1252

    Full Solution:

        String cmd = System.getenv("windir") + "\\system32\\" + "chcp.com";
        Process p = Runtime.getRuntime().exec(cmd);
        // Use default charset here - only want digits which are "core UTF8/UTF16"; 
        // ignore text preceding ":"
        String windowsCodePage = new Scanner(
            new InputStreamReader(p.getInputStream())).skip(".*:").next();
    
        Charset charset = null;
        String[] charsetPrefixes = 
            new String[] {"","windows-","x-windows-","IBM","x-IBM"};
        for (String charsetPrefix : charsetPrefixes) {
            try {
                charset = Charset.forName(charsetPrefix+windowsCodePage);
                break;
            } catch (Throwable t) {
            }
        }
        // If no match found, use default charset
        if (charset == null) charset = Charset.defaultCharset();
    
        cmd = System.getenv("windir") + "\\system32\\" + "tasklist.exe";
        p = Runtime.getRuntime().exec(cmd);
        InputStreamReader isr = new InputStreamReader(p.getInputStream(), charset);
        BufferedReader input = new BufferedReader(isr);
    
        // Debugging output
        System.out.println("matched codepage "+windowsCodePage+" to charset name:"+
                charset.name()+" displayName:"+charset.displayName());
        String line;
        while ((line = input.readLine()) != null) {
               System.out.println(line);
        }
    

    Thanks for the Q! – was fun.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The sqlite3.7 is giving this issue specifically for windows box on NTFS file system
Solution I did some googling and found this forum post , and here is
I found this post, windows service startup timeout But just wanted to clarify, I
This post asks this question but doesn't really give an answer, so I thought
I dont think this is possible in c# but ill post it anyway. Given
This post shows how to axiomatise a length function for Z3's built-in lists. However,
This post on SO answers most of the questions I have (much thanks to
This post relates to this: Add row to inlines dynamically in django admin Is
This post started as a question on ServerFault ( https://serverfault.com/questions/131156/user-receiving-partial-downloads ) but I determined
This post is incorrectly tagged 'send' since I cannot create new tags. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.