Barry Barry - 4 months ago 59
Java Question

Maven exec plugin ClassNotFoundException

I'm using the Maven exec plugin to run a java application from the command line with the command mvn exec:java. I have specified the main class in the pom.xml and the associated dependencies.

<groupId>com.example.MyApp</groupId>
<artifactId>MyApp</artifactId>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.myclass</mainClass>
<arguments>
<argument>configFile</argument>
<argument>properties</argument>
</arguments>
</configuration>
</plugin>


I also specify a number of dependencies...

<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.example.MyLibrary</groupId>
<artifactId>MyLibrary</artifactId>
<version>1.0.0</version>
</dependency>


The
MyApp
program reads a config file which is passed in as a command line argument. The config file contains the name of a class which is located in
MyLibrary
. So the class could be
com.mypackage.driver.MyClass
which is located in
MyLibrary
which is a dependency of the
MyApp
jar listed above.
However when I try to run this I get a
ClassNotFoundException
...

Update----
I'm using the system classloader to load the classes which are passed in on the command line for the
MyApp
program

ClassLoader loader = ClassLoader.getSystemClassLoader();


I'm thinking that this is causing the problem as it is looking for the classes on the default classpath which does not contain the dependencies.

Any hints on what I'm doing wrong here?

Answer

Are you still looking for an answer to this question? I had the exact same issue, and finally figured it out.

You need to add includePluginDependencies to your configuration to make the plugin search your dependencies for the main class:

<configuration>
  <includePluginDependencies>true</includePluginDependencies>
  <mainClass>com.example.myclass</mainClass>
  <arguments>
    <argument>configFile</argument>
    <argument>properties</argument>
  </arguments>
</configuration>

See here: http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies

Comments