0

Summary: How to run an Eclipse Java Maven Project outside of Eclipse

Details:

I am a Maven newbie and I have created a very simple Maven project in Eclipse.

The Package Explorer view of my project is as follows:

enter image description here
The source code for the two simple Java files is as follows:

package Pkg01;

public class Calculator
{
  public double add(double number1, double number2)
  {
    return number1 + number2;
  }

  public static void main(String[] args) {
      Calculator calculator = new Calculator();

      System.out.println(calculator.add(100, 200));
  }
}

and

package Pkg01;

import junit.framework.TestCase;

public class TestCalculator extends TestCase
{
  public void testAdd()
  {
    Calculator calculator = new Calculator();
    double result = calculator.add(10, 50);
    assertEquals(60, result, 0);
  }
}  

From within Eclipse I am able to run the Java Application, the Junit Test and the Maven test, without any issues.

My question is how can I invoke the mvn command from the DOS shell so that I can:

  1. Run the Java Application
  2. Run the Junit Test
  3. Run the Maven test

BTW running mvn exec:java from the parent directory of the project is not executing the Java application for me.

My pom.xml file looks like this:

<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>org.newbie</groupId>
  <artifactId>Stackoverflow</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Stackoverflow</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project> 

mvn exec:java gives the following error:

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< org.newbie:Stackoverflow >----------------------
[INFO] Building Stackoverflow 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ Stackoverflow ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.957 s
[INFO] Finished at: 2019-11-05T22:47:00-06:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project Stackoverflow: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java are missing or invalid -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginParameterException  

Here are the reasons why I feel that my question is not a duplicate.

I created a very simple project with maven-archetype-quickstart and my environment is correct because everything {i.e. (1) Run the Java Application, (2) Run the Junit Test and (3) Run the Maven test} works for me within the Eclipse IDE environment. The other poster was having trouble executing, which I don’t.

The comments that the other poster received were mainly for fixing his environment to get it running. I don't have that issue since, as already mentioned, everything works for me within the Eclipse IDE environment.

All I am asking is what I need to do get the following things working from the command shell:

  1. Running the Java Application
  2. Running the Junit Test
  3. Running the Maven test
Sandeep
  • 1,245
  • 1
  • 13
  • 33
  • Does your `pom.xml` include the `mainClass` and related elements configuring the `exec-maven-plugin` plug-in so that it knows what class to run? – nitind Nov 06 '19 at 05:03
  • can you post the contents of your pom.xml and .classpath? – riruzen Nov 06 '19 at 05:05
  • When you run `mvn exec:java`, what do you see? Post whatever message you get. – Ladlestein Nov 06 '19 at 05:05
  • I have edited the original post to include `pom.xml` as well as the error I encounter when I execute `mvn exec:java` – Sandeep Nov 06 '19 at 05:14
  • Possible duplicate of [Maven & Java: The parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java are missing or invalid](https://stackoverflow.com/questions/19850956/maven-java-the-parameters-mainclass-for-goal-org-codehaus-mojoexec-maven-p) – seenukarthi Nov 06 '19 at 05:38

1 Answers1

1

The error message states parameters 'mainClass' for goal org.codehaus.mojo:exec-maven-plugin.If you look at the exec:java documentation the required parameter is mainClass

I don't see exec-maven-plugin being configured in your pom.xml so you could execute Java programs using below command

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

Make sure you have compiled you code either using mvn compile or mvn install command before running mvn exec.. command

C:\data\development\app_code\my-app>mvn exec:java -Dexec.mainClass="com.mycompany.app.App"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ my-app ---
300.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.960 s
[INFO] Finished at: 2019-11-06T09:55:23+00:00
[INFO] Final Memory: 9M/116M
[INFO] ------------------------------------------------------------------------
C:\Sachin\data\development\app_code\test-maven\my-app>

For Junit testing you could call mvn test which would give you the test results status similar to your Eclipse IDE

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.mycompany.app.AppTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.099 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.198 s
[INFO] Finished at: 2019-11-06T09:55:58+00:00
[INFO] Final Memory: 19M/160M
[INFO] ------------------------------------------------------------------------

I do see the same details being mentioned in the link by Karthikeyan Vaithilingam

Reference:

https://www.mojohaus.org/exec-maven-plugin/usage.html


OP Addendum
You have provided the solution for:
1. Running the Java Application
3. Running the Maven test

From the IDE I can also invoke:
2. Running the Junit Test

Admittedly this is very similar to 3. Running the Maven test but the output looks different.

enter image description here

Is there a way to do that from outside of the IDE environment?

Sandeep
  • 1,245
  • 1
  • 13
  • 33
Sachin
  • 2,087
  • 16
  • 22