Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run java program using Maven

I am trying to run a Java project using Maven, need help on how to run

I tried with various options, Run As > Maven clean, Run As > Maven Install, Run As > Maven test, etc. But the output is not showing in the console though build is successful

I am using eclipse and able to run java file using Run As > Java Application

My Build tag in pom.xml

 <build>
    <plugins>
        <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
                <source>1.6</source>
                <target>1.6</target>
        </configuration>
     </plugin>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
       <version>1.6.0</version>
        <configuration>
             <mainClass>mypackage.classnamehavingmain</mainClass>
        </configuration>
        </plugin>
    </plugins>
   </build>
like image 650
saikrishna Avatar asked Sep 17 '19 18:09

saikrishna


People also ask

Can a Maven project run as Java application?

Run as Java Application - We can run any Java application/project using this option. It doesn't matter whether project is Maven or Ant or Gradle. It should work if we have main method in a class.


2 Answers

You should add the goal for execution

 <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <executions>
          <execution>
            <goals>
              <goal>your_goal_name</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.example.Main</mainClass>
        </configuration>
      </plugin>

and then to execute you can perform :

 mvn exec:your_goal_name
like image 197
EFOE Avatar answered Oct 16 '22 19:10

EFOE


If you would like to run it from the terminal you can do mvn compile exec:java -Dexec.mainClass=mypackage.classnamehavingmain.

like image 33
Aladejubelo Oluwashina Avatar answered Oct 16 '22 18:10

Aladejubelo Oluwashina