Search code examples
javaseleniumselenium-webdrivertestngtestng-dataprovider

How to run testng.xml using command prompt


I was trying to run testng.xml from command prompt but not able to figure out the following error:

Error: Could not find or load main class org.testng.TestNG

or either

Error: Could not find or load main class java.

While using two different method.

Please provide a prominent method to do the same.


Solution

  • Thanks guys, but i have solve this problem using Apache ant.

    By using following - build.xml

    <project name="name" basedir=".">
        <!-- ========== Initialize Properties =================================== -->
        <!-- set global properties for build -->
        <property name="basedir" value="." />
        <property name="lib" value="${basedir}/lib" />
        <property name="src" value="${basedir}/src" />
        <property name="bin" value="${basedir}/bin" />
        <property name="report-dir" value="${basedir}/Test-Report" />
        <property name="testng-report-dir" value="${report-dir}/TestNGreport" />
    
        <!-- ====== Set the classpath ====  -->
        <path id="classpath">
            <pathelement location="${bin}" />
            <fileset dir="${lib}">
                <include name="*.jar" />
            </fileset>
        </path>
    
        <!-- Delete directories  -->
    <target name="delete-dir">
            <delete dir="${bin}" />
            <delete dir="${report-dir}" />
        </target>
    
        <!-- Creating directories -->
     <target name="create" depends="delete-dir">
            <mkdir dir="${bin}" />
            <mkdir dir="${report-dir}" />
        </target>
    
        <!-- Compile the java code from ${src} into ${bin} -->
        <target name="compile" depends="create">
            <javac srcdir="${src}" classpathref="classpath" includeAntRuntime="No" destdir="${bin}" />
            <echo> /* Compiled Directory Classes */ </echo>
        </target>
    
        <!-- Runs the file and generates Reportng report for TestNG-->
        <taskdef name="testng" classname="org.testng.TestNGAntTask" classpathref="classpath" />
    
        <target name="testng-execution" depends="compile">
            <mkdir dir="${testng-report-dir}" />
            <testng outputdir="${testng-report-dir}" classpathref="classpath" useDefaultListeners="true">
                <xmlfileset dir="${basedir}" includes="testng.xml" />
            </testng>
        </target>
    </project>
    

    And then by typing: ant testng-execution on command prompt