Java – set the project to UTF-8 encoding, and the default character set returns windows-1252

I have a coding problem I'm not sure if it's ide related, but I'm using NetBeans 7.4 I got this code in the 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

When I run it, it will never display normally UTF - 8 should print out, but it's not When I try:

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

It returns windows-1252 This item is set to UTF - 8 encoding I even tried to resave this particular Java file in UTF - 8, but it still didn't display properly

On the other hand, I try to create a J2SE project that displays correctly when I run the same code The default character set also returns UTF-8

Both projects have UTF - 8 codes

I want my J2EE project to run like J2SE Until I update my java to version 1.7 0_ I only noticed this problem before 51-b13, but I'm not sure if it's relevant

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

I also tried to set the default encoding for the entire ide: - j-dfile Encoding = UTF-8, but it doesn't help

I noticed an important fact When I create a new web application, it displays normally When I created a new Maven web application, it did not display correctly

The same problem is found here: https://netbeans.org/bugzilla/show_bug.cgi?id=224526

I haven't fixed it yet There is still no solution

In my POM In XML, the encoding is set correctly, but windows-1252 is still displayed in the end

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

Solution

I spent hours trying to find the best solution

First of all, this is a maven problem. Even if you have specified different coding, it will choose platform coding and use it Maven doesn't seem to care (it even prints to the console. It uses UTF-8, but it won't display normally when you run the file with the above code)

I try to solve this problem by setting system variables:

JAVA_ TOOL_ OPTIONS = -Dfile. encoding = UTF8

Instead of setting a system variable, there should be another option, which is to set it as an additional compiler parameter

For example, javac - dfile encoding = UTF8

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>