Search code examples
javaxmlmavenexec-maven-plugin

exec-maven-plugin could not find or load main class but output runs fine on the command line


I try to use the exec-maven-plugin to run a Java program.

I use the following pom snippet:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
             <configuration>
                    <executable>java</executable>
                        <arguments>
                         <argument>-Dmyproperty=myvalue</argument>
                            <argument>-cp</argument>
                            <argument>"/home/vogella/libs/*"</argument>
                            <argument>com.vogella.test.Main</argument>
                        </arguments>
    </configuration>


</plugin>

The class com.vogella.test.Main is contained in one of the jar files which are located in /home/vogella/libs/*. If I run the mvn -X clean install exec:exec command, I see the following error message:

[DEBUG] Executing command line: java -Dmyproperty=myvalue -cp "/home/vogella/libs/*" com.vogella.test.Main Error: Could not find or load main class com.vogella.test.Main

If I copy the command line (java -Dmyproperty=myvalue -cp "/home/vogella/libs/*" com.vogella.test.Main) in the shell from which I started the Maven build, then the Java program is executed correctly.

Any idea what is wrong with my Maven setup?


Solution

  • With CLI, the /home/vogella/libs/* expression is expanded by bash and resolves to the list of files. With Maven, the expression is directly executed and not expanded. so it remains "/home/vogella/libs/*" which is not a valid jar file. You'll probably have more success by using the antrun plugin and use the java Ant task in the script. Ant understands wildcards better than anything else.