2

I was able to run this in Eclipse but have problem running it with command line.

My main java class that I want to run is Example.java (inside src/main/java/examples)
Note that although there is a simple junit test in the main Example.java I don’t want to have any tests associated with the java file in src/test/java )

First I cd to the directory where pom.xml is

cd pathToTheProject/projectname

I do the following:

mvn clean
mvn compile
mvn package

Now to only run the main class called Example.class I write the following (I suppose pathToMavenLib/.m2/repository/* contains all the jar files needed):

java -cp "pathToMavenLib/.m2/repository/*" examples.Example

But this gives me error: Error: Could not find or load main class examples.Example

Edit:
I solve my problem using mvn exec:java -Dexec.mainClass="org.junit.runner.JUnitCore" -Dexec.arguments="examples.Example"

april
  • 131
  • 3
  • 11

1 Answers1

4

You can create a deployable artifact as war file (By changing packaging to war using <packaging>war</packaging>) that will contain all the dependent jar files under its lib directory.
You can then run: mvn clean install

You can then extract it and run your class file directly by giving class-path parameter.

Other option is to use maven run plugin - mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...

reference: Maven Run Project

Amit Mahajan
  • 895
  • 6
  • 34
  • Thanks for your reply. My main class doesn't have main method since it runs with junit. Do you know how do I refer to the main method in junit while using "mvn exec:java" command so I say something like mvn exec:java org.junit.runner.JUnitCore -Dexec.mainClass="examples.Example" but it gives me error – april Oct 09 '17 at 20:38
  • mvn -Dtest=TestCircle test – Amit Mahajan Oct 09 '17 at 20:40
  • thanks, now based on the main java class I provided in my question I tried to run mvn -Dtest=examples.Example testUnsatisfiableClasses but doesn't work. the command I wrote is true? – april Oct 09 '17 at 20:56
  • What is the error you get when you run it? Check your maven version and test plugin. Refer: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html https://www.mkyong.com/maven/how-to-run-unit-test-with-maven/ – Amit Mahajan Oct 09 '17 at 21:06
  • @april you are asking for trouble if you do not have a main method. – Thorbjørn Ravn Andersen Oct 09 '17 at 21:06
  • I solve the problem using mvn exec:java -Dexec.mainClass="org.junit.runner.JUnitCore" -Dexec.arguments="examples.Example" thanks for your help – april Oct 10 '17 at 01:44