Results 1 to 3 of 3
  1. #1
    Grismar is offline Member
    Join Date
    Feb 2011
    Location
    The Hague, the Netherlands
    Posts
    1
    Rep Power
    0

    Default UTF-8 from an Exec

    I've tried about all I could think of and Google is no help either. I have some files that include special characters in UTF-8 in their names. What I want to do is to run a 'dir' statement on the system command line, capture the output and return the resulting list of names. What I have is:

    Java Code:
    String[] cmd = {"cmd.exe", "/C", "dir /b /s /o:n " + filter};
    p = Runtime.getRuntime().exec( cmd );
    br = new BufferedReader (new InputStreamReader (p.getInputStream(), "Cp850"));
    String line;
    while ((line = br.readLine()) != null) {
      // do something
    }
    This works as expected but, as you may have noticed, it uses codepage 850. Anything that's not in that code, but is in UTF-8 like ellipsis, is changed to a period. If I switch this to:

    Java Code:
    br = new BufferedReader (new InputStreamReader (p.getInputStream(), "UTF-8"));
    It no longer works correctly, replacing all special characters with other characters. If I explicitly tell the command to use the UTF-8 codepage like this:

    Java Code:
    String[] cmd = {"cmd.exe", "/C", "chcp 65001 & dir /b /s /o:n " + filter};
    it still doesn't work. Even though this works just fine in a Windows command window. (provided I use a Unicode font like Lucida Console for the command window)

    So, I know the cmd.exe statement outputs UTF-8 correctly, I know the stream reading from the process is configured for reading UTF-8, so what am I doing wrong? I suspect Java somehow knows the command processor runs Cp850 by default and interprets the output as such - but how would I go about changing this? Does anyone know what is going on?

    Greetings,
    Grismar.

  2. #2
    Astara is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default

    Not a java expert (or user, really), but as you note, it works interactively.

    What is the default input encoding of your 'java'...I'd guess, likely on windows some limited code page.

    See if there's a call in java to change your locale to a UTF-8 locale, OR set
    chcp 65001 before running your java prog (I don't know if java is smart enough to realize cp65001=utf-8, I've seen bug reports on linux utils (gnu) that failed because it couldn't convert 'utf-8' to cp65001 (no conversion is needed, cp65001 IS UTF-8)...
    But it's MS-specific. So depends on how well ported your java is to the windows platform --- otherwise, look for a java specific way to READ file I/O that is UTF-8 encoded then try your last example (would have best chance of working as you know
    it works interactively!)...

    I only mention these ideas as perl attaches an encoding value to input and output stream. (it's not binary, by default, but whatever your environment is set to -- in Windows it is Unlikely to be UTF-8. So you'll need to make sure your I/O streams are in UTF-8.. (as well as enabling it in the external env 0-- like you do w/chcp 65001)...

    You may not see this answer, as it's 7 months past your post date, but thought I'd leave the suggestion/idea for anyone
    hitting page in a future websearch (as I did)...

    A*a

  3. #3
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Re: UTF-8 from an Exec

    Though this thread is pretty ancient right now..i just solved a quite similar problem and wanted to leave the answer for anyone finding this page in a future websearch (like I did)...

    The codepage switching needs to be done in a command separated from the dir-and-writing-filelist-stuff-command. This worked out for me:

    Java Code:
    ProcessBuilder pb = new ProcessBuilder();
    List<String> commands = Arrays.asList("cmd.exe","/c","chcp","65001","&","cmd.exe","/c","dir","/s","/b>"+tmpfile.getPath());
    pb.directory(directoryWhereToExecute);
    pb.command(commands);
    Process p = pb.start();
    Last edited by layouterlimits; 12-12-2012 at 12:20 PM.

Similar Threads

  1. exec not working in browser
    By tecno40 in forum Java Applets
    Replies: 3
    Last Post: 11-14-2010, 08:46 PM
  2. runtime.exec
    By cotede2 in forum Advanced Java
    Replies: 3
    Last Post: 04-17-2009, 05:18 PM
  3. help with Runtime.exec()
    By Lanfear in forum New To Java
    Replies: 18
    Last Post: 12-16-2008, 11:09 AM
  4. Runtime.exec()
    By hknyo in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •