Maven exec plugin ClassNotFoundException

Viewed 15658

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?

5 Answers

Try mvn clean compile exec:java

Sometimes we forget to compile the code and when you run mvn exec:java without compiling, then obviously maven cannot find the compiled class files and complain about ClassNotFoundException

Related