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?
Below command works for me. Provided that all required jars including testng jar kept inside lib.
You need to have the
testng.jar
under classpath.try
C:\projectfred> java -cp "path-tojar/testng.jar:path_to_yourtest_classes" org.testng.TestNG testng.xml
Update:
Under linux I ran this command and it would be some thing similar on Windows either
Directory structure:
Once I compile all sources they go under bin directory. So, in the classpath I need to specify contents of bin directory and all the libraries like testng.xml, loggers etc over here. Also copy testng.xml to bin folder if you dont want to specify the full path where the testng.xml is available.
Update
:Go to the folder
MyProject
and type run the java command like the way shown below:-I believe the testng.xml file is under
C:\Users\me\workspace\MyProject
if not please give the full path fortestng.xml
fileI had faced some 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
Java -cp C:\Users\User123\TestNG\lib*;C:\Users\User123\TestNG\bin org.testng.TestNG testng.xml
That's it !
Let me know if you have any more concerns.
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:.
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..
this should run your testNG cases.
Prepare
MANIFEST.MF
file with the following contentPack all test and dependency classes in the same jar, say
tests.jar
Notice trailing ".", replace
folder-with-classes/
with proper folder name or path.Create
testng.xml
with content like belowReplace
com.example.yourcompany.qa.Test1
with path to your Test class.Run your tests