2

I am working on a project and I can't compile that generates XMLBeans from a WSDL.

The files are generated ok, but when I want to compile the project, I have problems with a few classes.

The main problem, I guess, is that the name of the class has an ñ, AñoDocument.java and it doesn't compile.

[INFO] Compiling 998 source files to D:\workspace3\MyProject\my_module\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] \workspace3\MyProject\my_module\src\main\java\com\mycompany\schema\AñoDocument.java:[17,18] illegal character: \65533
[ERROR] \workspace3\MyProject\my_module\src\main\java\com\mycompany\schema\AñoDocument.java:[20,63] illegal character: \65533
[ERROR] \workspace3\MyProject\my_module\src\main\java\com\mycompany\schema\AñoDocument.java:[20,77] <identifier> expected
[ERROR] \workspace3\MyProject\my_module\src\main\java\com\mycompany\schema\AñoDocument.java:[20,82] = expected
[ERROR] \workspace3\MyProject\my_module\src\main\java\com\mycompany\schema\AñoDocument.java:[20,83] ';' expected
....

In Eclipse -> Project -> Properties -> Resource -> Text Encoding I have checked "Inherit from container (Cp1252)".

This is the POM of my project:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
      <artifactId>MyProject</artifactId>
      <groupId>com.mycompany</groupId>
      <version>1.0</version>
    </parent>

    <groupId>com.mycompany</groupId>
    <artifactId>my_module</artifactId>
    <version>1.0</version>
    <name>my_module</name>
    <url>http://maven.apache.org</url>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>xmlbeans-maven-plugin</artifactId>
                <version>2.2.0</version>
                <executions>
                    <execution>
                       <phase>generate-sources</phase>
                        <goals>
                            <goal>xmlbeans</goal>
                        </goals>
                    </execution>
                </executions>
                <inherited>true</inherited>
                <configuration>
                    <schemaDirectory>downloads</schemaDirectory>
                    <noVDoc>true</noVDoc>
                    <noJavac>true</noJavac>
                    <sourceGenerationDirectory>src/main/java</sourceGenerationDirectory>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>     
                        <fileset>
                        <directory>src/main/java</directory>
                            <includes>
                                <include>**/*.java</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin> 
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans-xpath</artifactId>
            <version>2.2.0</version>
        </dependency>   

        <dependency>
            <groupId>net.sf.saxon</groupId>
            <artifactId>saxon</artifactId>
            <version>8.7</version>
        </dependency>

    </dependencies>

</project>

Does any one have any idea about how can I do to fix this?

Sled
  • 18,541
  • 27
  • 119
  • 168
iVela
  • 1,160
  • 1
  • 19
  • 40

3 Answers3

1

I don't think the problem is with the ñ character itself. This is a perfectly valid character for a class name, as demonstrated by the following test class (which I know prints true on JDK 1.6).

public class Main {
    public static void main(String[] args) {
         System.out.println(Character.isJavaIdentifierPart('ñ'));
    }
}

You are saving the file in Cp1252, which is compatible with UTF-8 for ASCII characters, but not for non-ASCII. Then you tell maven that your source files are encoded in UTF-8. This causes maven to read the ñ character incorrectly, which appears in your build when it interprets ñ as the character \65533 - the replacement character. Try to change the Resource encoding to UTF-8 under Eclipse -> Project -> Properties -> Resource -> Text Encoding

Ryan Gross
  • 6,423
  • 2
  • 32
  • 44
  • I tried as you said, but when I run mvn generate-sources, the files are generated wrong. ie: public interface A�oDocument extends org.apache.xmlbeans.XmlObject – iVela Jul 12 '11 at 03:40
  • So... it's XmlBeans that you need to tell how to encode the source. I don't see any controls on the plugin, or on XmlBeans itself. It probably uses the default encoding of the environment created by Maven. Maybe you can change that? Maybe it's the encoding Maven was given at startup (normal JVM)? Or... you already know my other suggestion - just say no to ñ ;-) I added an xmlbeans tag in case an XmlBeans maven can help, but other questions on it are slow to get answered, so I doubt it. – Ed Staub Jul 12 '11 at 17:09
  • does the encoding of the input file matter? which encoding has it? – Daniel Kutik Jul 12 '11 at 18:36
0

From the Java Language Spec:

The Java letters include uppercase and lowercase ASCII Latin letters A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or \u0024). The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems.

In other words, it's illegal to have an n-tilde on a class name. Which means that many tools are likely to be unhappy with it in a Java filename as well.

Update See comments below. Looks like this is completely wrong. Would delete it, but the comments are worthwhile.

Ed Staub
  • 15,480
  • 3
  • 61
  • 91
  • I don't think that is true. The spec also says: `A "Java letter-or-digit" is a character for which the method Character.isJavaIdentifierPart returns true.` See my sample code below, which shows that ñ is fine. – Ryan Gross Jul 11 '11 at 21:19
  • I believe that the problem is the ñ because I have many other classes that are being compiled right. And when maven tríes to compile this class it fails... – iVela Jul 11 '11 at 22:38
  • @Ryan - I agree that the spec is self-contradictory here. Also, if you read the javadoc or impl for [isJavaIdentifierPart](http://download.oracle.com/javase/1,5.0/docs/api/java/lang/Character.html#isJavaIdentifierPart%28char%29), you'll see that the character passes by virtue of being an isIdentifierIgnorable() character. This suggests real problems when it comes time to map the class name to a filename, or vice versa. In mapping to a filename, it may be ambiguous; and mapping the other way is impossible. It _may_ work, but if it were me, I'd just stay the heck away - but I speak English. – Ed Staub Jul 11 '11 at 22:48
  • @Ryan (cont) To check this out, I'd try making a class with an accent, compile it, and see what the .class filename is. Sorry, I'm linguistically challenged or I'd do it myself. – Ed Staub Jul 11 '11 at 22:52
  • @Ed Staub: `Character.isJavaIdentifierPart('ñ')); //true` `Character.isIdentifierIgnorable('ñ'); // false` – Ryan Gross Jul 12 '11 at 00:09
  • @nacho3d: I agree that it is the ñ. I think the problem is that the encoding that you save your file in is different than the one you told maven to build with. You should try changing the project setting in Eclipse to save the file in UTF-8. – Ryan Gross Jul 12 '11 at 00:12
  • @Ed Staub: Yes, the .java file. The post mentioned it was saved in Cp1252. – Ryan Gross Jul 12 '11 at 00:33
0

I solved in this way: 1) close eclipse, 2) mvn clean, 3) mvn compile

iVela
  • 1,160
  • 1
  • 19
  • 40