Barry
Barry

Reputation: 593

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?

Upvotes: 7

Views: 16807

Answers (5)

Girivasan
Girivasan

Reputation: 364

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

Upvotes: 1

Caleb
Caleb

Reputation: 524

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

Upvotes: 18

Moritz Petersen
Moritz Petersen

Reputation: 13057

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>

Upvotes: 1

Jens Baitinger
Jens Baitinger

Reputation: 2365

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>

Upvotes: 2

jjmartinez
jjmartinez

Reputation: 807

Before to execute from command line, you should import also in command line your library. You can use this command with your expecific name and info of your library:

mvn install:install-file -Dfile=MyLibrary.jar -DgroupId=com.example.MyLibrary -DartifactId=MyLibrary -Dversion=1.0.0 -Dpackaging=jar

Upvotes: 0

Related Questions