【问题标题】:Change default charset to UTF-8将默认字符集更改为 UTF-8
【发布时间】:2014-03-22 10:38:07
【问题描述】:

我有一个带有 maven builder 的 java 项目。现在我需要支持 UTF-8 字符集。我不知道为什么默认字符集(Charset.defaultCharset())总是US-ASCII. 我将 Pom.xml 配置更改为 UTF-8(用于编码)并设置 -Dfile.encoding=UTF-8 但对于 Unicode 字符,输出始终为“?”(例如,"Việt Nam" => Vi?t Nam)。

我在 Ant buidler 上查过,它是正确的 UTF-8 字符。

这是我的 Pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <outputEncoding>UTF-8</outputEncoding>
                <argLine>-Dfile.encoding=UTF-8</argLine>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <id>cucumber</id>
                    <phase>test</phase>
                    <configuration>
                        <executable>src/scripts/cucumber.sh</executable>
                        <commandlineArgs>${host} ${port} ${profile}</commandlineArgs>
                    </configuration>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

谢谢

更新 我的系统是

NetBeans 7.3.1 JDK 1.7 Mac OS 10.9.1

更新 我把IDE换成Intelij IDEA,问题就解决了,不知道是什么原因。

【问题讨论】:

    标签: java maven utf-8 character-encoding


    【解决方案1】:

    &lt;build&gt;之前试试这个

    <project xmlns="...">
    ... 
    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.resources.sourceEncoding>UTF-8</project.resources.sourceEncoding>
    </properties>
    

    【讨论】:

    • 我曾尝试将&lt;project.resources.sourceEncoding&gt;UTF-8&lt;/project.resources.sourceEncoding&gt; 添加到属性中,但没有任何改变。
    【解决方案2】:

    不确定我的问题是否正确,但您似乎没有遇到 maven 和源代码问题,而是 正在运行的程序和用户输入,对吗?

    如果是这样,你应该在运行你的程序的 JVM 上设置-Dfile.encoding=UTF-8,而不是在运行 maven 的 JVM 上编译你的程序。您粘贴的配置仅确保 maven 和 javac 将您的源文件解释为 UTF-8 编码。

    有关您的问题的更多详细信息,另请参阅this question and aswers

    hth, - 马丁

    【讨论】:

    • 谢谢,我试过了,但没有任何反应。我解决了从 Netbeans 将 IDE 更改为 IDEA 的问题。
    【解决方案3】:

    这为我解决了问题。将以下内容添加到 html 或 jsp 中。

    <!DOCTYPE html>
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    
    <html lang="en">
    ...
    

    【讨论】:

      猜你喜欢
      • 2011-03-31
      • 1970-01-01
      • 2019-07-31
      • 2011-09-05
      • 1970-01-01
      • 2010-11-12
      • 2012-08-26
      • 2015-04-14
      • 2017-02-27
      相关资源
      最近更新 更多