Tuan Chau
Tuan Chau

Reputation: 1333

Change default charset to UTF-8

I have a project in java with the maven builder. Now I need to support the UTF-8 charset. I don't know why the default charset (Charset.defaultCharset()) is always US-ASCII. I changed the Pom.xml configuration to UTF-8 (for encoding) and set -Dfile.encoding=UTF-8 but the output is always "?" for Unicode's chars (eg. "Việt Nam" => Vi?t Nam) .

I had checked it on Ant buidler, it's correct as UTF-8's characters.

This is my 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>

Thank you

Update My system is

NetBeans 7.3.1 JDK 1.7 Mac OS 10.9.1

Update I have change the IDE to Intelij IDEA, and the problem is gone, but I don't know why.

Upvotes: 4

Views: 5625

Answers (3)

Han
Han

Reputation: 449

This fixed it for me. Add the below into the html or jsp.

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

<html lang="en">
...

Upvotes: 1

Martin H&#246;ller
Martin H&#246;ller

Reputation: 2869

Not sure if I got your problem right, but it seems you are not having a problem with maven and the source code but with the running program and the user input, right?

If so, you shoud set -Dfile.encoding=UTF-8 on the JVM running your program and not the JVM running maven to compile your program. The configuration you pasted only makes sure maven and the javac interpret your source-files as UTF-8 encoded.

See also this question and aswers for more details on your problem.

hth, - martin

Upvotes: 1

Min Naing Oo
Min Naing Oo

Reputation: 1095

Try this before <build>

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

Upvotes: 2

Related Questions