简体   繁体   English

将项目编码设置为UTF-8,默认字符集返回windows-1252

[英]Encoding for project set to UTF-8, default charset returns windows-1252

I've ran into an issue with encoding. 我遇到了编码问题。 Not sure if it's IDE related but I'm using NetBeans 7.4. 不确定它是否与IDE有关,但我使用的是NetBeans 7.4。 I got this piece of code in my J2EE project: 我在J2EE项目中得到了这段代码:

    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 never displays properly. 当我运行它时,它永远不会正常显示。 UTF-8 should be able to print that out ok but it doesn't. UTF-8应该可以打印出来,但不是。 Also when I tried: 当我尝试时:

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

it returned windows-1252. 它返回了windows-1252。 The project is set to UTF-8 encoding. 该项目设置为UTF-8编码。 I've even tried resaving this specific java file in UTF-8 but it still doesn't display properly. 我甚至尝试在UTF-8中重新保存这个特定的java文件,但它仍然无法正常显示。

I've tried to create J2SE project on the other hand and when I run the same code it displays properly. 另一方面,我试图创建J2SE项目,当我运行相同的代码时,它正确显示。 Also the default charset returns UTF-8. 默认字符集也返回UTF-8。

Both projects are set the UTF-8 encoding. 两个项目都设置了UTF-8编码。

I want my J2EE project to act the same like the J2SE one. 我希望我的J2EE项目像J2SE一样运行。 I didn't notice this issue until I updated my java to version 1.7.0_51-b13 but again I'm not sure if that is related. 直到我将我的java更新到版本1.7.0_51-b13之前我才注意到这个问题但是我不确定这是否相关。

I'm experiencing the same issue like this guy: http://forums.netbeans.org/ptopic37752.html 我遇到了像这个家伙一样的问题: http//forums.netbeans.org/ptopic37752.html

I've also tried setting the default encoding for the whole IDE: -J-Dfile.encoding=UTF-8 but it didn't help. 我也尝试过为整个IDE设置默认编码:-J-Dfile.encoding = UTF-8但它没有帮助。

I've noticed an important fact. 我注意到一个重要的事实。 When I create a new web application it displays ok. 当我创建一个新的Web应用程序时,它显示正常。 When I create new Maven web application it displays incorrectly. 当我创建新的Maven Web应用程序时,它显示不正确。

Found the same issue here: https://netbeans.org/bugzilla/show_bug.cgi?id=224526 在这里发现了同样的问题: https//netbeans.org/bugzilla/show_bug.cgi?id = 224456

I still haven't fixed it yet. 我还没有修好它。 There's still no solution working. 仍然没有解决方案。

In my pom.xml the encoding is set properly, but it still shows windows-1252 in the end. 在我的pom.xml中,编码设置正确,但最后仍显示windows-1252。

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

I've spend few hours trying to find the best solution. 我花了几个小时试图找到最好的解决方案。

First of all this is an issue of maven which picks up platform encoding and uses it even though you've specified different encoding to be used. 首先,这是一个maven的问题,即使您已指定要使用的不同编码,它也会选择平台编码并使用它。 Maven doesn't seem to care (it even prints to console that it's using UTF-8 but when you run a file with the code above, it won't display properly). Maven似乎并不关心(它甚至打印到控制台,它使用的是UTF-8,但是当您使用上面的代码运行文件时,它将无法正常显示)。

I've managed to tackle this issue by setting a system variable: 我设法通过设置系统变量来解决这个问题:

JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 JAVA_TOOL_OPTIONS = -Dfile.encoding = UTF8

There should be another option instead of setting system variables and that is to set it as additional compiler parameter. 应该有另一个选项而不是设置系统变量,即将其设置为附加的编译器参数。

like javac -Dfile.encoding=UTF8 比如javac -Dfile.encoding = UTF8

You are mixing a few concepts here: 你在这里混合了一些概念:

  • the project encoding is the encoding used to save the Java source files (xxxx.java) - it has nothing to do with how your code executes 项目编码是用于保存Java源文件(xxxx.java)的编码 - 它与代码的执行方式无关
  • test.getBytes("UTF-8") returns a series of bytes representing your String in UTF-8 encoding test.getBytes("UTF-8")返回一系列字节,表示UTF-8编码的字符串
  • to recreate the original string, you need to explicitly give the encoding, unless it is the default of your machine: new String(test.getBytes("UTF-8"), StandardCharsets.UTF_8) 要重新创建原始字符串,您需要显式地给出编码,除非它是您的机器的默认值: new String(test.getBytes("UTF-8"), StandardCharsets.UTF_8)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM