Encoding for the project, set to UTF-8, the default charset windows-1252 returns

I had a problem with the encoding. Not sure if this is due to IDE, but I'm using NetBeans 7.4. I got this piece of code in my J2EE project:

String test = "kukuřičné"; System.out.println(new String(test.getBytes("UTF-8"))); // should display ok System.out.println(new String(test.getBytes("ISO-8859-1"))); System.out.println(new String(test.getBytes("UTF-16"))); System.out.println(new String(test.getBytes("US-ASCII"))); System.out.println(new String(test.getBytes("windows-1250"))); System.out.println(test); // should display ok 

And when I run it, it will never be displayed properly. UTF-8 should be able to print is normal, but it is not. Also, when I tried:

  System.out.println(Charset.defaultCharset()); 

He returned windows-1252. The project is set to UTF-8 encoding. I even tried to keep this particular file java in UTF-8, but it still does not display properly.

I tried to create a J2SE project, on the other hand, and when I run the same code, it is displayed correctly. Also, the default encoding is UTF-8 returns.

Both projects set UTF-8 encoding.

I want my J2EE project worked as well as J2SE. I did not notice the problem until the updated my java to version 1.7.0_51-b13, but again, not sure if it is connected.

I am experiencing the same problem as this guy: http://forums.netbeans.org/ptopic37752.html

I also tried to set the default encoding for all IDE: -J-Dfile.encoding = UTF-8, but it did not help.

I noticed an important fact. When I create a new Web application, it is displayed normally. When I create a new Maven web application, it is not displayed correctly.

I found the same problem: https://netbeans.org/bugzilla/show_bug.cgi?id=224526

I still haven't fixed it. still not working solution.

In my pom.xml, the encoding is set correctly, but at the end, window-1252 is still displayed.

 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
+6
source share
2 answers

I spent several hours trying to find the best solution.

First of all, it is a problem the maven, which selects the encoding platform and uses it, even if you specify a different character encoding to be used. Maven, did not seem to care (he even print console, it uses UTF-8, but when you run the file with the above code, it will not be displayed correctly).

I managed to solve this problem by setting the system variable:

JAVA_TOOL_OPTIONS = -Dfile.encoding = UTF-8

Instead of installing the system variables to be another parameter which must be specified as an additional parameter compiler.

how javac -Dfile.encoding = UTF8

+8
source

Here you mix several concepts:

  • Project encoding - the encoding used to save the Java source files (xxxx.java) - it has nothing to do with how your code runs.
  • test.getBytes("UTF-8") returns a sequence of bytes representing your string in UTF-8 encoding
  • to re-create the original string, you need to explicitly specify the encoding, if it is not the default setting for your machine: new String(test.getBytes("UTF-8"), StandardCharsets.UTF_8)
+1
source

All Articles