javamavenmaven-2maven-exec-plugin

when I run java Maven Project from cmd I get MojoExecutionException


I have problem by running Maven-Project from command_line I used this command in cmd to run class named mainClass

   mvn -e exec:java -Dexec.mainClass="com.example.Main"

and here is what I have in my pom file:

   <project xmlns="http://maven.apache.org/POM/4.0.0"     
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>Linux</groupId>
   <artifactId>Linux</artifactId>
   <version>1.0</version>

   <dependencies>
       <dependency>
           <groupId>org.seleniumhq.selenium</groupId>
           <artifactId>selenium-java</artifactId>
           <version>2.37.1</version>
       </dependency>

       <dependency>
           <groupId>org.codehaus.groovy</groupId>
           <artifactId>groovy-all</artifactId>
           <!-- Needs to be the same version that REST Assured depends on -->
           <version>2.1.2</version>
           <scope>test</scope>
       </dependency>

       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.8.1</version>
       </dependency>
   </dependencies>

    </project>

I get Mojo Exception caused by class not found exception, but actually I don't know where I should provide the name or the path to the class I want to run

this is the response of the command in command-line

enter image description here

enter image description here


Solution

  • I have modified your Maven pom.xml and added Maven mojo plugin. You can find below the code.

    First you have to build project and then you can execute the Main java class. Here, follow the steps.

    1. Go to command prompt and go to the project/directory containing pom.xml.
    2. Type the command mvnw clean package. I have used maven wrapper.
    3. Then type command mvnw exec:java -Dexec.mainClass="com.so.help.maven.Main"

    You can find the sample project in github.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>execute-java-maven</groupId>
        <artifactId>execute-java-maven</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <name>execute-java-maven</name>
        <url>http://www.example.com</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.9</version>
            </dependency>
    
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>2.37.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <!-- Needs to be the same version that REST Assured depends on -->
                <version>2.1.2</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
            </dependency>
        </dependencies>
    
        <build>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.6.1</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.6.0</version>
                        <configuration>
                            <executable>java</executable>
                            <arguments>
                                <argument>com.so.help.maven.Main</argument>
                            </arguments>
                        </configuration>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    </project>