9

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.

Community
  • 1
  • 1
roesslerj
  • 2,611
  • 5
  • 30
  • 44
  • Setting the default encoding of the platform to UTF-8 resolved the problem. I assume this is a bug in Maven. – roesslerj Jun 01 '12 at 00:10
  • Which version of the compiler-plugin do you use? Have you defined the project.build.SourceEnconding in your pom (it looks you didn't) – khmarbaise Jun 01 '12 at 07:23
  • The version of the compiler-plugin is not specified in the pom, Maven is version "2.2.1 (rdebian-4)". I tried to specify the `project.build.SourceEnconding` property in the pom, didn't change anything. And this should be the same as giving `-Dproject.build.sourceEncoding=` in the command line anyway. – roesslerj Jun 01 '12 at 08:51
  • 2
    Than you should try to define the maven-compiler-plugin version in pluginManagement (version:2.5) and recheck...cause in maven-compiler-plugin version 2.1 had been a fix for the econding (http://jira.codehaus.org/secure/ReleaseNote.jspa?projectId=11130&version=12304) that might affect you. But i'm not sure which version of the maven-compiler-plugin is defined in MVN 2.2.1. – khmarbaise Jun 01 '12 at 09:38
  • @khmarbaise: If you make this an official answer, I'll accept it. If you don't I will :-) – roesslerj Sep 07 '12 at 10:05

2 Answers2

22

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>
Sairam Krish
  • 10,158
  • 3
  • 55
  • 67
  • No, the only global setting of coding is to be done by env. variable: https://stackoverflow.com/a/9976788/715269 – Gangnus Mar 09 '20 at 15:34
7

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)

Community
  • 1
  • 1
Jens
  • 6,243
  • 1
  • 49
  • 79