Ashok kumar Ashok kumar - 1 month ago 16
Java Question

Unable to run the Ant file

When I run the ant file through command prompt, I can get an error like:

C:\Users\workspace\TestNGProject\Config>ant -buildfile reportng-build.xml Testng Buildfile: C:\Users\workspace\TestNGProject\Config\reportng-build.xml

init:[delete] Deleting directory C:\Users\workspace\TestNGProject\Config\bin-dir
[mkdir] Created dir: C:\Users\workspace\TestNGProject\Config\bin-dir
[delete] Deleting directory C:\Users\workspace\TestNGProject\Config\ht mlreport
[mkdir] Created dir: C:\Users\workspace\TestNGProject\Config\html-report

compile:

Testng:
[mkdir] Created dir: C:\Users\workspace\TestNGProject\Config\htmlreport\TestNG-report
[testng] java.lang.NoClassDefFoundError:com/beust/jcommander/ParameterExcept ion
[testng] at java.lang.Class.getDeclaredMethods0(Native Method)
[testng] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
[testng] at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
[testng] at java.lang.Class.getMethod0(Class.java:3018)
[testng] at java.lang.Class.getMethod(Class.java:1784)
[testng] at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
[testng] at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.j ava:526)
[testng] Caused by: java.lang.ClassNotFoundException: com.beust.jcommander.Pa rameterException
[testng] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[testng] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

[testng] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[testng] ... 7 more [testng] Error: A JNI error has occurred, please
check your installation and try again [testng] The tests failed.

BUILD SUCCESSFUL Total time: 0 seconds


Ant file:

<project name="Testng Ant build" basedir=".">
<!-- Sets the property varaibles to point to respective directories -->
<property name="report-dir" value="${basedir}/html-report" />
<property name="testng-report-dir" value="${report-dir}/TestNG-report" />
<property name="lib-dir" value="${basedir}/lib" />
<property name="bin-dir" value="${basedir}/bin-dir" />
<property name="src-dir" value="${basedir}" />

<!-- Sets the classpath including the bin directory and all the jars under the lib folder -->
<path id="test.classpath">
<pathelement location="${bin-dir}" />
<fileset dir="${lib-dir}">
<include name="*.jar" />
</fileset>
</path>
<!-- Deletes and recreate the bin and report directory -->
<target name="init">
<delete dir="${bin-dir}" />
<mkdir dir="${bin-dir}" />
<delete dir="${report-dir}" />
<mkdir dir="${report-dir}" />
</target>

<!-- Compiles the source code present under the "srcdir" and
place class files under bin-dir -->
<target name="compile" depends="init">
<javac srcdir="${src-dir}" classpathref="test.classpath"
includeAntRuntime="No" destdir="${bin-dir}" />
</target>

<!-- Defines a TestNG task with name "testng" -->
<taskdef name="testng" classname="org.testng.TestNGAntTask"
classpathref="test.classpath">
<classpath>
<pathelement location="C:/Users/Downloads/Jars/testng- 6.9.9.jar"/>
</classpath>
<classpath>
<pathelement location="C:/Users/Downloads/Jars/jcommander-1.48.jar"/>
</classpath>
</taskdef>

<!--Executes the testng tests configured in the testng.xml file-->
<target name="Testng" depends="compile">
<mkdir dir="${testng-report-dir}" />
<testng outputdir="${testng-report-dir}"
classpathref="test.classpath" useDefaultListeners="false"
listeners="org.uncommons.reportng.HTMLReporter">
<!-- Configures the testng xml file to use as test-suite -->
<xmlfileset dir="${basedir}" includes="testng.xml" />
<sysproperty key="org.uncommons.reportng.title" value="ReportNG Report" />
</testng>
</target>
</project>


I have added the jcommander jar file and the class pathelementlocation also. But still it is not working. Guide me to reach out.

Answer Source

I got it. Actually in the file set tag I mentioned the default path

<fileset dir="${lib-dir}">

example: C:/Project/Jars

But in my project I didn't create the exact path as it was mentioned in file set dir

In project I kept the project structure like C:/Project/Jars/lib

Now I changed the library path in the project structure to C:/Project/Jars and it works now.