1

I have problem with encoding in Java. I have set encoding in eclipse UTF-8. When I run my app from eclipse everything is ok but when I exported to jar and run it by double clicking I have ???? characters. When I run from command line: java -jar app.jar everything is ok. The problem is with downloaded data from other site (the site is utf8 encoded). What's the solution ?

EDIT: On all platforms, when I run from command line the defaultEncoding() is UTF-8. But when I run by double clicking: Mac: US-ASCII Windows: windows-1250

I have wrote encoding method but it still not working:

public String getPageContent(String url) throws MalformedURLException, IOException
{
    URL urlReader;
    InputStream response = null;
    BufferedReader reader;
    String pageContent = "";
    urlReader = new URL(url);
    response = urlReader.openStream();
    reader = new BufferedReader(new InputStreamReader(response));


    for (String line; (line = reader.readLine()) != null;) {
        pageContent += this.encode(line, "UTF-8");

    }   
    reader.close();
    return pageContent;        
}
public String encode(String s, String charset)
{
        try {
            byte[] b = s.getBytes(charset);
            s = new String(b, charset);
            return s;
        } catch (UnsupportedEncodingException e1) {


    // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return s;

}

mitch
  • 2,235
  • 3
  • 27
  • 46

2 Answers2

2

You need to specify the UTF-8 character set when you construct the InputStreamReader.

reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));

You shouldn't be trying to re-encode strings after having received them at all.

davmac
  • 20,150
  • 1
  • 40
  • 68
0

Setting the default Java character encoding? Here is already a discussed thread with more details. Hope this help

Community
  • 1
  • 1
greatmajestics
  • 1,083
  • 4
  • 19
  • 41
  • Thank you for response but I am not interested in Input/Output stream. I don't use filesystem. The problem is elsewhere. – mitch Oct 17 '12 at 07:47