Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVN compile not using UTF-8 encoding

Ok, this is a weird problem: I have a java test file that uses some UTF-8 characters. When I compile it with Maven, using

mvn -Dfile.encoding=UTF-8 -Dproject.build.sourceEncoding=UTF-8 test

(thus setting both the perceived platform encoding and the source file encoding, see maven platform encoding) I get something like

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building project
[INFO]    task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory path/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory path/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 7 source files to path/target/test-classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
path/to/file.java:[42,23] unclosed character literal
path/to/file.java:[42,25] ';' expected
path/to/file.java:[42,26] unclosed character literal
path/to/file.java:[47,23] unclosed character literal
path/to/file.java:[47,25] illegal character: \182
path/to/file.java:[47,26] unclosed character literal

When I compile the file with

javac path/to/file.java

I get similar errors:

path/to/file.java:42: unclosed character literal
    illegalCharEnc('ä');
                   ^
path/to/file.java:42: ';' expected
    illegalCharEnc('ä');
                     ^
path/to/file.java:42: unclosed character literal
    illegalCharEnc('ä');
                      ^
path/to/file.java:47: unclosed character literal
    illegalCharDec('ö');
                   ^
path/to/file.java:47: illegal character: \182
    illegalCharDec('ö');
                     ^
path/to/file.java:47: unclosed character literal
    illegalCharDec('ö');
                      ^
6 errors

Now when I use

javac -encoding UTF-8 path/to/file.java

instead, I get cannot find symbol errors, because of the missing dependencies. So I figure the problem is that javac is not called with UTF-8 option in Maven when compiling tests (notice how Using 'UTF-8' encoding to copy filtered resources. is missing in compiler:testCompile-section). Is this conclusion correct? Am I missing something? Is this a known problem? Anything I can do about it? Obviously, the platform encoding on my system is not UTF-8, but I currently cannot change that.

like image 754
roesslerj Avatar asked May 31 '12 23:05

roesslerj


2 Answers

Maven compiler plugin, takes encoding as a configuration parameter. Not only UTF-8 but a wide variety of encoding standards are supported.,

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
like image 73
Sairam Krish Avatar answered Sep 20 '22 07:09

Sairam Krish


Having had the same problem I came across this question first. But as there was no answer I looked further and found another question that was answered and helped me solving this:

https://stackoverflow.com/a/10375505/332248 (credit to @chrisapotek and @Jopp Eggen for answering this)

like image 42
Jens Avatar answered Sep 21 '22 07:09

Jens