How to Run testng.xml File Without using IDE or Build Tool?
The article shows how you can run our testng.xml file without using any IDE (Eclipse, NetBeans, etc.) or Build tools (Ant or Maven). You often face a situation where you are assigned a new Virtual machine and you are asked to run your created Selenium scripts on it without any IDE or Build tool. In addition, you are also expected to compile and run the scripts using the command prompt.
If you want to run your testng.xml files without any IDE or build tool, first you need to install Java on the system. In addition, you also need to set its path into environment variable.
Let’s consider the below-mentioned code for a Selenium project.
-> libs (all supported jar files)
-> src1 (java code source directory)
-> tests (this folder contains .java files)
-> src2 (java code source directory)
-> tests1 (this folder contains .java files))
First, you need to compile Java code using the command prompt. For this, you need to open the command prompt and go to the PROJECT_HOME directory.
In order to compile the code, you need to type the following command:
javac -classpath libs/* -d ./bin ./src1/com/example/tests/*.java ./src2/com1/example1/tests1/*.java
- “javac” is used to compile .java files available in the bin folder of the jdk.
- Option ‘-classpath’ points the supported required files. In the above code, ‘-classpath libs/*’ is used to find the required jar files from ‘libs’ directory. If you want to include more than one classpath, you can use semicolon “;”. Example: -classpath lib1/*;lib2/*;lib3/*. Alternatively, you can also use ‘-cp’ in place of ‘-classpath’.
- “-d” stands for the “directory“. It explains the compiler that where the class files should be created. In the above given code, –d is passed as a ‘bin’ directory. So all .class files will go into ‘bin’ directory after compilation.
Last argument “./src1/com/example/tests/*.java ./src2/com1/example1/tests1/*.java” is the complete path, where the Java file exists.
Note: Above command will work on all operating systems.
Code Compilation on Windows Machine
You can also compile the code on a Windows machine. For this, open the command prompt and reach at project directory. Then, type command the following command:
dir /s /B *.java > javaFilePath.txt
The above command will search all the .java files in current and subdirectory and put the entire Java file path in ‘javaFilePath.txt’ file. You can compile the entire Java file using the below-mentioned command.
javac -classpath libs/* -d ./bin @javaFilePath.txt
For Linux System $ find -name “*.java” > sources.txt$ javac -classpath libs/* -d ./bin @javaFilePath.txt Note: You should not put any space while naming the directory; else the above compilation command will not work.
Run the testing.xml file
To run the testing.xml file, you can use the following command:
java -cp bin;libs/* org.testng.TestNG TestNG/testng.xml