Maven exec plugin ClassNotFoundException

I am using the Maven exec plugin to launch a Java application from the command line using the mvn exec: java command. I pointed out the main class in pom.xml and related 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 indicate the 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> 

MyApp reads the configuration file, which is passed as a command line argument. The configuration file contains the name of the class located in MyLibrary . So the class could be com.mypackage.driver.MyClass , which is 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 use the system class loader to load classes that are passed on the command line for MyApp

 ClassLoader loader = ClassLoader.getSystemClassLoader(); 

I think this is causing the problem as it is looking for default classes that do not contain dependencies.

Any hints that I'm doing wrong here?

+6
source share
4 answers

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

You need to add includePluginDependencies in your configuration in order to make the plugin look for 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

+12
source

You can let the generation of the classpath be generated as follows:

  <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> 
0
source

Before you execute from the command line, you must also import your library on the command line. You can use this command with your detailed name and information about your library:

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

You need to add the dependency as a dependency of the execution plugin, so that the execution plugin can load the com.example.myclass class you 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> 
0
source

All Articles