How exactly do I run a .java TestNG project from a command line?
I have read through the TestNG documentation, and tried the following to no avail:
C:\projectfred> java org.testng.TestNG testng.xml
... with the following testng.xml file in my project:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteAll" verbose="1">
<test name="TestAll">
<packages>
<package name="com.project.fred.tests"/>
</packages>
</test>
</suite>
The error I get is this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.testng.TestNG. Program will exit.
Obviously, I am not referencing TestNG correctly in my command line. Does anyone know how to get this working?
This question is related to
java
command-line
testng
command-prompt
Ok after 2 days of trying to figure out why I couldn't run the example from
http://www.tutorialspoint.com/testng/testng_environment.htm the following code did not work for me
C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
The fix for it is as follows: I came to the following conclusion: First off install eclipse, and download the TestNG plugin. After that follow the instructions from the tutorial to create and compile the test class from cmd using javac, and add the testng.xml. To run the testng.xml on windows 10 cmd run the following line:
java -cp C:\Users\Lenovo\Desktop\eclipse\plugins\org.testng.eclipse_6.9.12.201607091356\lib\*;C:\Test org.testng.TestNG testng.xml
to clarify: C:\Users\Lenovo\Desktop\eclipse\plugins\org.testng.eclipse_6.9.12.201607091356\lib\*
The path above represents the location of jcommander.jar and testng.jar that you downloaded by installing the TESTNG plugin for eclipse. The path may vary so to be sure just open the installation location of eclipse, go to plugins and search for jcommander.jar. Then copy that location and after that add * to select all the necessary .jars.
C:\Test
The path above represents the location of the testing.xml in your project. After getting all the necessary paths, append them by using ";".
I hope I have been helpful to some of you guys :)
Below command works for me. Provided that all required jars including testng jar kept inside lib.
java -cp "lib/*" org.testng.TestNG testng.xml
Thanks a lot it was really very helpful.. I was able to run my TestNG classes using command line, but still I would like to wrap-up the things one last time as follows..
1) Set the classpath for the testng.jar in the environment variables
(either manually or via command line).
2) Create a new folder "MyTest".
3) Compile all your classes(testNG+java) and put them(.class files )
in a new folder "Mytest\bin".
4) Put your 'testng.xml' and '.properties' files to "Mytest\bin"
again.
5) Put all the .jar files (testng,selenium... etc) in a new folder
"Mytest\lib".
6) Browse to the folder "bin" via command line and run the command
java -cp "..\lib\*;" org.testng.TestNG testng.xml
this should run your testNG cases.
After gone throug the various post, this worked fine for me doing on IntelliJ Idea
:
java -cp "./lib/*;Path to your test.class" org.testng.TestNG testng.xml
Here is my directory structure:
/lib
-- all jar including testng.jar
/out
--/production/Example1/test.class
/src
-- test.java
testing.xml
So execute by this command:
java -cp "./lib/*;C:\Users\xyz\IdeaProjects\Example1\out\production\Example1" org.testng.TestNG testng.xml
My project directory Example1
is in the path:
C:\Users\xyz\IdeaProjects\
I had faced same problem because I downloaded TestNG plugin from Eclipse. Here what I did to get the Job done :
After adding TestNG to your project library create one folder in your Project names as lib ( name can be anything ) :
Go to "C:\Program Files\Eclipse\eclipse-java-mars-R-win32-x86_64\eclipse\plugins" location and copy com.beust.jcommander_1.72.0.jar and org.testng_6.14.2.r20180216145.jar file to created folder (lib).
Note : Files are testng.jar and jcommander.jar
That's it !
Let me know if you have any more concerns.
If none of the above answers work, you can run the test in IDE, get the class path and use it in your command. Ex: If you are using Intellij IDEA, you can find it at the top of the console(screenshot below).
Clicking on the highlighted part expands and displays the complete class path.
you need to remove the references to jars inside the folder: JetBrains\IntelliJ IDEA Community Edition 2019.1.3
java -cp "path_copied" org.testng.TestNG testng.xml
I tried everything on this post but it turns out I was missing a jar file. Here's my solution:
Add guice-4.2.2.jar to your jar files if you are getting the following error. If you are using maven then add the following dependency.
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.1.0</version>
</dependency>
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/inject/Stage
at org.testng.internal.Configuration.<init>(Configuration.java:33)
at org.testng.TestNG.init(TestNG.java:216)
at org.testng.TestNG.<init>(TestNG.java:200)
at org.testng.TestNG.privateMain(TestNG.java:1312)
at org.testng.TestNG.main(TestNG.java:1304)
Caused by: java.lang.ClassNotFoundException: com.google.inject.Stage
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 5 more
Prepare MANIFEST.MF
file with the following content
Manifest-Version: 1.0
Main-Class: org.testng.TestNG
Pack all test and dependency classes in the same jar, say tests.jar
jar cmf MANIFEST.MF tests.jar -C folder-with-classes/ .
Notice trailing ".", replace folder-with-classes/
with proper folder name or path.
Create testng.xml
with content like below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Tests" verbose="5">
<test name="Test1">
<classes>
<class name="com.example.yourcompany.qa.Test1"/>
</classes>
</test>
</suite>
Replace com.example.yourcompany.qa.Test1
with path to your Test class.
Run your tests
java -jar tests.jar testng.xml
You will need to use semicolon as delimiter while specifying the jar and class file path in windows. This solved the issue.
Assuming the class file is under C:.
java -cp ".;C:\testng.jar" org.testng.TestNG testing.xml
If you are using Maven, you can run it from the cmd line really easy, cd into the directory with the testng.xml (or whatever yours is called, the xml that has all the classes that will run) and run this cmd:
mvn clean test -DsuiteXmlFile=testng.xml
This page explains it in much more detail: How to run testng.xml from Maven command line
I didn't know it mattered if you were using Maven or not so I didn't include it in my search terms, I thought I would mention it here in case others are in the same situation as I was.
Using " ; " as delimiter on windows issue got resolved.
java -cp "path of class files; testng jar file path" org.testng.TestNG testng.xml
ex:-
java -cp ".\bin;..\common_lib\*;" org.testng.TestNG testng.xml
Source: Stackoverflow.com