Search code examples
javaeclipsemavendependenciesbuildpath

Running Eclipse project which uses another Eclipse Project that has a Maven dependency


I have a problem with running an Eclipse project using another Eclipse project which has a Maven dependency.
(I am new to Maven so there might be essential things I do wrong or forgot to do)

Problem environment

mapdat > ordinary Eclipse project
config > Eclipse project having 1 maven dependency (just 1 class with static functions)

mapdat has config added to its build path (Project > Properties > Java Build Path > Projects).

The config project was created as an ordinary Eclipse project.
Now I need the JSON library org.json inside config.

I want to have this library provided over a Maven dependency, so I did following:

  • Converted the config project to a Maven Project
  • config > Maven > Update Project
  • config > Run As > Maven install
  • config > Maven > Add Dependency > org.json 20160212 with scope compile (also tried with provided)
  • config > Maven > Update Project
  • config > Run As > Maven install
  • pom.xml of config:

    <project>
    ...
        <modelVersion>4.0.0</modelVersion>
        <groupId>ch.audacus.config</groupId>
        <artifactId>config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source/>
                        <target/>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20160212</version>
            </dependency>
        </dependencies>
    </project>
    

    Everything seems fine; no errors nor warnings.

    Problem

    When I run config directly there is no error and I can make use of the org.json library.

    If I try to run mapdat project which uses the config project which has the Maven dependency (org.json), I get following error:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
        at ch.audacus.config.Config.readConfig(Config.java:45)
        at ch.audacus.mapdat.Editor.initEditor(Editor.java:35)
        at ch.audacus.mapdat.Editor.<init>(Editor.java:28)
        at ch.audacus.mapdat.Editor.main(Editor.java:73)
    Caused by: java.lang.ClassNotFoundException: org.json.JSONObject
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 4 more
    

    Did I set up the Maven dependency properly?
    Is a special run configuration needed?
    Do I have to specify goals in the Maven project config?
    Do I have to add special sources to the mapdat project?

    Thanks for helping...


    Solution

    I converted mapdat into a Maven project just as O'Roan suggested in the answer and did the described steps (maven clean and maven install).

    One important part of the solution was to add a JDK 1.8 as an "Installed JRE" under Preferences > Java > Installed JREs (as default) and checking the Execution Environments settings (... > Installed JREs > Execution Environments). Afterwards I set the Preferences > Java > Compiler Compiler compliance level to 1.8. Then I set the source and target configuration in the pom also to 1.8. This helped fixing issues with running Maven clean and Maven install.

    There was also a problem with the JRE System Library that was set on the project when an Maven project update was done:
    After a Maven project update it automatically set J2SE-1.5 as JRE System Library which didn't work for a project using Java 8. This problem was caused by the missing source and target configuration in the pom.

    Current working poms:

    config

    <project>
    ...
        <modelVersion>4.0.0</modelVersion>
        <groupId>ch.audacus.config</groupId>
        <artifactId>config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20160212</version>
            </dependency>
        </dependencies>
    </project>
    

    mapdat

    <project>
    ...
        <modelVersion>4.0.0</modelVersion>
        <groupId>ch.audacus.mapdat</groupId>
        <artifactId>mapdat</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>ch.audacus.config</groupId>
                <artifactId>config</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </project>
    

    Thanks.


    Solution

  • You might want to convert mapdat to a maven project and add config as a maven dependency instead. Then run > maven clean and run > maven install on mapdat. That should take care of all the dependencies and work.