I am wondering about to create correct Bat file ,
I am using Maven + Selenium(TestNG)
I have tried many different ways to Resolve exception, But its one of the most common exception: Could not find or load main class / java.lang.ClassNotFoundException
In my Maven project there is no bin or lib folder, And I am wondering for it. Whether to call library from .m2(Maven) installation.
To call class file, In my maven project .class file located something like : E:\testBatchDemo\target\classes\mytest
Here is sample of What I have tried so far,
set projectLocation=E:\testBatchDemo
cd %projectLocation%
set classpath=E:\testBatchDemo\target\classes\mytest;
java -cp %classpath% C:\Users\Desktop-pc\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar; C:\Users\Desktop-pc\.m2\repository\org\seleniumhq\selenium\selenium-java\3.12.0\selenium-java-3.12.0.jar; org.testng.TestNG %projectLocation%\testng.xml
pause
Anyone can please assist, Which library need to call or How can I achieve correct .class path.
As per your question description I think there are certain grey areas which we need to address beofre jumping into the solution.
- I am wondering about to create correct
batch
(i.e. .bat
) file: Fundamentally, Maven is a build tool which extensively works in-conjunction with pom.xml
and doesn't requires any Windows batch file.
- Moving forward if your usecase is to pass the Windows batch file to Jenkins for Continous Integration then you need to verify this step if the Windows batch file is able to kick-start the execution of your Test Suite.
- However Jenkins can also kick-start the execution of your Test Suite through the
pom.xml
and Maven.
Now, your usecase must match to one of the above mentioned requirement as mentioned below:
- Windows Batch file + TestNG
- Jenkins + TestNG
- Jenkins + Maven
As you have extensively spoken about the Windows batch file so here are the relevant details as per the first option Windows Batch file + TestNG:
Write a simple code block with @Test
annotation of TestNG as follows:
package demoJenkins;
import org.testng.annotations.Test;
public class DemoJenkinsJob
{
@Test
public void testJenkins()
{
System.out.println("Welcome to Jenkins World");
}
}
Run As TestNG Test
- On Successful execution convert using
TestNG -> Convert to TestNG
and testng.xml
gets created.
The testng.xml
will look like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="demoJenkins.DemoJenkinsJob"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Get the absolute Project Location from your IDE (i.e. Eclipse), browse to the sub-directory, create a directory lib
Copy all the relevant jars and libraries (Selenium and TestNG jars) in the lib directory.
selenium-server-standalone-3.13.0.jar
org.testng_6.14.2.r201802161450.jar
com.beust.jcommander_1.72.0.jar
org.apache-extras.beanshell.bsh_2.0.0.b6.jar
org.yaml.snakeyaml_1.17.0.jar
Through CLI browse to the Project Directory and provide the following classpath:
>set classpath=<Project Directory>\bin;<Project Directory>\lib\*;
Through CLI execute testng.xml
as follows:
Project_Directory>java org.testng.TestNG testng.xml
On successful execution, within the Project Directory create a new text document and add the following code:
java -cp bin;lib/* org.testng.TestNG testng.xml
Save the file as run.bat
- Execute the Windows batch file run.bat and verify it executes the Test Suite as expected.
Update
As per your question and subsequent comment,
When you use TestNG and/or Maven, these framework/buildtool keeps all the relevant jars in their respective location. But when you intend the configure the buildpath manually you have to create the lib directory manually and keep a copy of the relevant jars.
Perhaps you don't need to create the bin directory manually as on successful compilation of your Java program your program will automatically create the bin and keeps the class file before execution.