7

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 5

18

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have the same issue, added includePluginDependencies, still ClassNotFoundException. Any other suggestions?
Also adding includePluginDependencies the error continue to face out. The reason was "clean" goal. In my case just remove it and it works. @MaxiWu
2

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>

1 Comment

Thanks for the response, I've added the dependency to the plugin, but it's still giving me a classnotfound error. I've followed this example here mojo.codehaus.org/exec-maven-plugin/examples/…
1

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>

3 Comments

Hi @Moritz can you elaborate further on this, I was under the impression that once I ran mvn exec:java then the dependencies that were defined by this project would automatically be included on the classpath...
Where do you put this? Inside the <plugin>?
yep, this is the config of the plugin
1

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

Comments

0

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

1 Comment

Hi Thanks for the response, the jar is located in my local repository already

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.