Run main class compiled with Maven from terminal - java

I have two main classes in my Maven based project (Spring Boot). I'm using <start-class> property in pom.xml to indicate the Spring Boot main class. The other one is a process that I need to run manually sometimes.
This is the project's structure:
Project (pom.xml dir) is in ~/projects/coolproject
Packages (containing the classes) are under ~/projects/coolproject/src/main/java
Main class I want to execute manually is ~/projects/coolproject/src/main/java/com/company/Process.java
After run mvn package, I want to run Process.class, but I'm getting Could not find or load main class error. I have tried this:
From ~/projects/coolproject/src/main/java, execute java com.company.Process
From ~/projects/coolproject/src/main/java, execute java -cp . com.company.Process
From ~/projects/coolproject/src/main/java/com/company, execute java Process
From ~/projects/coolproject/, execute java -cp target/coolproject-1.0.0-SNAPSHOT.jar com.company.Process
with no success. How can I do it?
I have tried to run it using Maven Exec plugin. From pom.xml's dir:
$ mvn exec:java -Dexec.mainClass="com.company.Process"
But it runs the other main class (Spring Boot's main class), instead. It's ignoring the mainClass param.

As mentioned in comments, while using spring boot, you should use start-class instead of mainClass as the parameter in exec plugin. Try:
mvn exec:java -Dstart-class=com.company.Process
Alternatively, if you want to use java -cp to run your main class, you need to pass the correct dependencies. It seems that coolproject-1.0.0-SNAPSHOT.jar isn't built with dependencies that com.company.Process needs. To build a jar with all dependencies, you can use maven-assembly-plugin. Then you can try:
mvn package
java -cp target/coolproject-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.mycompany.Process

Related

JUnit4-Serenity: Create an executable jar with all required dependencies and run the tests standalone

Please help solving the below:
Create an executable jar file of a Maven Java Project which only consists of JUnit4-Serenity tests and run it on command line.
Serenity-JUnit4 Maven Java Project - used only to run tests.
It only consists of test classes and each test class has an annotation #RunWith(SerenityRunner.class).
Currently I am executing all the test using the mvn command as below with couple of input parameters "appName" and "testENV":
mvn clean verify -DappName=test -DtestENV=integration
Reference project - https://github.com/serenity-bdd/serenity-junit-starter - running using Maven.
I am doing this to dockerize it by adding the created jar to a base image which consists of some required root CA.

How to run maven project using batch file?

This is what i have done till now:
I have API automation Script in maven using testng, Following is the structure of project
2.Now i first tried to run testng.xml using command-line with following command.
java -cp ".;C:\Users\A622965\.m2\repository\org\testng\testng\6.8\testng-6.8.jar" org.testng.TestNG testng.xml
3.But throws following error:
Error: Could not find or load main class org.testng.TestNG
Not able to figure out the issue after watching lot of tutorials.
I am looking to batch process the script using Task Schedular in Windows
Remember that Maven is a build tool, so every single operation which requires the classpath must be done through Maven, wether if you want to execute it from the command line or from some GUI.
So, in your case you will find useful the Maven command line tool:
mvn <phases>
In your case:
mvn test
But remember to include first in the pom.xml the testng library dependency, and also to properly configure the surefire plugin.
You need to build path for testng
Right click-->Build -->Libraries Tab-->Add External jar-->Then restart--it will work...

cmd error:could not find or load main class while executing core java in maven

I'm getting an error:
Could not find or load main class com.javatpoint.
App while executing a simple core java application of maven in command prompt given in javatpoint maven tutorial.
I'm able to compile the project but not able to run the project.
The link of the example
http://www.javatpoint.com/maven-example
Two options:
Add classpath before running your example. In the webpage is already written:
java -classpath target\CubeGenerator-1.0-SNAPSHOT.jar;.; com.javatpoint.App
or
Add main class in maven-jar-plugin
http://www.avajava.com/tutorials/lessons/how-do-i-specify-a-main-class-in-the-manifest-of-my-generated-jar-file.html
And then just run it:
java -jar myjarfile.jar

Run JUnit tests via command line? (Mac) [duplicate]

I would like to run JUnit test cases from the command line.
How can I do this?
For JUnit 5.x it's:
java -jar junit-platform-console-standalone-<version>.jar <Options>
Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher
For JUnit 4.X it's really:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
But if you are using JUnit 3.X note the class name is different:
java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name]
You might need to add more JARs or directories with your class files to the classpath and separate that with semicolons (Windows) or colons (UNIX/Linux). It depends on your environment.
Edit: I've added current directory as an example. Depends on your environment and how you build your application (can be bin/ or build/ or even my_application.jar etc). Note Java 6+ does support globs in classpath, you can do:
java -cp lib/*.jar:/usr/share/java/junit.jar ...
Write tests! :-)
Maven way
If you use Maven, you can run the following command to run all your test cases:
mvn clean test
Or you can run a particular test as below
mvn clean test -Dtest=your.package.TestClassName
mvn clean test -Dtest=your.package.TestClassName#particularMethod
If you would like to see the stack trace (if any) in the console instead of report files in the target\surefire-reports folder, set the user property surefire.useFile to false. For example:
mvn clean test -Dtest=your.package.TestClassName -Dsurefire.useFile=false
Gradle way
If you use Gradle, you can run the following command to run all your test cases:
gradle test
Or you can run a particular test as below
gradle test --tests your.package.TestClassName
gradle test --tests your.package.TestClassName.particularMethod
If you would like more information, you can consider options such as --stacktrace, or --info, or --debug.
For example, when you run Gradle with the info logging level --info, it will show you the result of each test while they are running. If there is any exception, it will show you the stack trace, pointing out what the problem is.
gradle test --info
If you would like to see the overall test results, you can open the report in the browser, for example (Open it using Google Chrome in Ubuntu):
google-chrome build/reports/tests/index.html
Ant way
Once you set up your Ant build file build.xml, you can run your JUnit test cases from the command line as below:
ant -f build.xml <Your JUnit test target name>
You can follow the link below to read more about how to configure JUnit tests in the Ant build file:
https://ant.apache.org/manual/Tasks/junit.html
Normal way
If you do not use Maven, or Gradle or Ant, you can follow the following way:
First of all, you need to compile your test cases. For example (in Linux):
javac -d /absolute/path/for/compiled/classes -cp /absolute/path/to/junit-4.12.jar /absolute/path/to/TestClassName.java
Then run your test cases. For example:
java -cp /absolute/path/for/compiled/classes:/absolute/path/to/junit-4.12.jar:/absolute/path/to/hamcrest-core-1.3.jar org.junit.runner.JUnitCore your.package.TestClassName
The answer that #lzap gave is a good solution. However, I would like to add that you should add . to the class path, so that your current directory is not left out, resulting in your own classes to be left out. This has happened to me on some platforms. So an updated version for JUnit 4.x would be:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Ensure that JUnit.jar is in your classpath, then invoke the command line runner from the console
java org.junit.runner.JUnitCore [test class name]
Reference: junit FAQ
With JUnit 4.12 the following didn't work for me:
java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Apparently, from JUnit 4.11 onwards you should also include hamcrest-core.jar in your classpath:
java -cp .:/usr/share/java/junit.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore [test class name]
In windows it is
java -cp .;/path/junit.jar org.junit.runner.JUnitCore TestClass [test class name without .class extension]
for example:
c:\>java -cp .;f:/libraries/junit-4.8.2 org.junit.runner.JUnitCore TestSample1 TestSample2 ... and so on, if one has more than one test classes.
-cp stands for class path and the dot (.) represents the existing classpath while semi colon (;) appends the additional given jar to the classpath , as in above example junit-4.8.2 is now available in classpath to execute JUnitCore class that here we have used to execute our test classes.
Above command line statement helps you to execute junit (version 4+) tests from command prompt(i-e MSDos).
Note: JUnitCore is a facade to execute junit tests, this facade is included in 4+ versions of junit.
If your project is Maven-based you can run all test-methods from test-class CustomTest which belongs to module 'my-module' using next command:
mvn clean test -pl :my-module -Dtest=CustomTest
Or run only 1 test-method myMethod from test-class CustomTest using next command:
mvn clean test -pl :my-module -Dtest=CustomTest#myMethod
For this ability you need Maven Surefire Plugin v.2.7.3+ and Junit 4.
More details is here: http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
Actually you can also make the Junit test a runnable Jar and call the runnable jar as
java -jar
Personally I would use the Maven surefire JUnit runner to do that.
Alternatively you can use the following methods in JunitCore class http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html
run (with Request , Class classes and Runner) or runClasses from your java file.

How to execute a Maven build Java application

I am playing to Maven and tried to built a simple HellowWorld application.
This application uses Spring to libraries.
When I tried to run it, I run it through:
\target\classes
with command:
java -cp HelloWorldApp
It has a long list of classpath dependencies.
I think maven must have some more clever ways to do this instead of listing a whole list of dependency libs.
Can someone help?
Update:
Thanks. I now have another question. I run the project using:
mvn exec:java -Dexec.mainClass="com.vaannila.HelloWorldApp"
However, my project uses a Spring config called beans.xml which is in the
\src\main\resources
When I run it, it says:
Caused by: java.io.FileNotFoundException: class path resource [beans.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:142)at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.jav
a:336)
How can I specify where to look the Spring config?
Many thanks
Run your application by issuing mvn exec:java at the command line, maven will take care of the rest including download of the maven exec plugin.
EDIT As for your updated question: It appears that maven did not copy your resources to the target folder, you can use the maven-resources-plugin to do that. This link should help you get this done.
If you use IDE, such as eclipse +m2eclipse - it will calculate all dependencies from maven dependencies.
If you are running from command line use Exec maven plugin

Categories