Search code examples
javaintellij-ideajbox2d

Can't build JBox2D duplicate class(es)


I'm interested in learning JBox2D, but I seem to have stumbled at the first hurdle - building the library.

The Quick-start instructions specify the following simple steps:

  1. Check out the project through Subversion:
  2. Import to your IDE as a Maven project (using the pom.xml descriptor in the root folder)
  3. Run the org.jbox2d.testbed.framework.TestbedMain class

I've checked out the code and imported the project in to IntelliJ IDEA (12.1.4), however, when I try to run the TestbedMain class (and it subsequently compiles the code) I get the following errors:

java: duplicate class: org.jbox2d.common.PlatformMathUtils
java: duplicate class: org.jbox2d.common.Timer

Duplicate class error

What am I doing wrong/have I missed?

Thanks


Solution

  • Looking at the POM for the jbox2d-library module, I see that there is a build section which explicitly ignores the classes under the gwtemul package:

    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <excludes>
                <exclude>**/gwtemul/**</exclude>
              </excludes>
            </configuration>
          </plugin>
        </plugins>
    </build>
    

    It seems to me that I should probably be building this library through MVN and attaching the built JAR as a dependency for the jbox2d-testbed module.

    However, I found that modifying the package declaration for these classes also solves the problem:

    package org.jbox2d.gwtemul.org.jbox2d.common;
    
    /**
     * A GWT-compatible implementation of the platform math utilities.
     */
    class PlatformMathUtils {
    
      public static final float fastPow(float a, float b) {
        return (float) Math.pow(a, b);
      }
    }
    

    And since they would have been ignored by the build anyway, I don't see that it causes any harm.

    Once I did this, I was able to run the org.jbox2d.testbed.framework.TestbedMain class and I get the expected GUI:

    TestbedMain Running