Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven exec plugin ClassNotFoundException

Tags:

java

maven

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?

like image 304
Barry Avatar asked Dec 20 '13 14:12

Barry


3 Answers

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

like image 151
Caleb Avatar answered Nov 13 '22 16:11

Caleb


You need to add the dependency as dependency of the execution plugin, so the execution plugin can load the class you configured com.example.myclass:

<plugins>
 <plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>exec-maven-plugin</artifactId>
   <version>1.2.1</version>
   [...]
   <dependencies>
     <dependency>
       <groupId>com.example.MyLibrary</groupId>
       <artifactId>MyLibrary</artifactId>
       <version>1.0.0</version>
       <type>jar</type>
     </dependency>

 </plugin>
like image 21
Jens Baitinger Avatar answered Nov 13 '22 18:11

Jens Baitinger


You can let the classpath get generated like this:

    <configuration>
      <executable>java</executable>
      <arguments>
        <argument>-Dmyproperty=myvalue</argument>
        <argument>-classpath</argument>
        <!-- automatically creates the classpath using all project dependencies,
             also adding the project build directory -->
        <classpath/>
        <argument>com.example.Main</argument>
        ...
      </arguments>
    </configuration>
like image 42
Moritz Petersen Avatar answered Nov 13 '22 18:11

Moritz Petersen