Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 594
vogella Avatar asked Jan 06 '14 12:01

vogella


People also ask

Could not find or load main class main class?

When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.

Could not find or load main class while executing jar file?

When the JVM is unable to locate the main class, it's often because it's looking for the corresponding . class files in the wrong classpath. Of course, the way to rectify this problem is to manually specify the classpath by either using packages or specifying the classpath.


2 Answers

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.

like image 144
Mickael Avatar answered Sep 25 '22 08:09

Mickael


You need to set the classpath through dependencies. With the commandline argument -cp you set the classpath explicitly but this does not work for the maven cp. This is constructed through dependencies.

<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <includeProjectDependencies>false</includeProjectDependencies>
                <includePluginDependencies>true</includePluginDependencies>
                <mainClass>org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher</mainClass>
                <arguments>
                    <argument>${project.basedir}/src/my/mavenized/GenerateHeroLanguage.mwe2</argument>
                </arguments>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.xtext</groupId>
                    <artifactId>org.eclipse.xtext.xtext</artifactId>
                    <version>2.5.0-SNAPSHOT</version>
                </dependency>
                <dependency>
                    <groupId>org.eclipse.xtext</groupId>
                    <artifactId>org.eclipse.xtext.xbase</artifactId>
                    <version>2.5.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </plugin>
like image 20
user3165406 Avatar answered Sep 22 '22 08:09

user3165406