Java – Maven exec plug-in classnotfoundexception
I am using the Maven exec plug-in to run a Java application from the command line. The command is MVN exec: Java I'm already in POM XML specifies the main class 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 specified some 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 configuration file, which is passed in as a command line parameter The configuration file contains the name of the class located in MyLibrary So this class can be com mypackage. driver. MyClass, located in MyLibrary, is a dependency of myapp jar listed above However, when I try to run it, I get a classnotfoundexception
- - update the class I'm using the system class loader to load the class passed in from the command line of the myapp program
ClassLoader loader = ClassLoader.getSystemClassLoader();
I think this is causing problems because it is looking for classes that do not contain dependencies on the default classpath
What did I do wrong here?
Solution
Are you still looking for the answer to this question? I had exactly the same question and finally figured it out
You need to add includeplugindependencies in the configuration to enable the plug-in to search for dependencies of the main class:
<configuration> <includePluginDependencies>true</includePluginDependencies> <mainClass>com.example.myclass</mainClass> <arguments> <argument>configFile</argument> <argument>properties</argument> </arguments> </configuration>
See: http://mojo.codehaus.org/exec-maven-plugin/java-mojo.html#includePluginDependencies