How to run testng.xml using command prompt - java

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.

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

Paths are separated using : under Unix-like systems and not ; as in Windows:
java -cp ./src/lib/*:./bin org.testng.TestNG testng.xml
If you are using bash under Windows, you should use ; and any other place use :.
The ; character means end of statement to a Unix shell, so what you are probably attempting to execute is:
java -cp ./src/lib/*
./bin org.testng.TestNG testng.xml

I use testNg on windows, in the windows command prompt the execution, I use the command: java -cp bin;libs/* org.testng.TestNG testng.xml
Result: test run successfully
always on windows: I use git bash command, the command does not execute successfully

Try to run via cmd prompt, like this:
1. Go to project folder
2. Then: `java -cp [projectpath]\lib\*;[projectpath]\target org.testng.TestNG testng.xml`

Related

Error: Could not find or load main class OCI_HelloWorld

I am to new java, ant and to stackoverflow.
I have a build.xml file under Project_Dir director in windows and run "ant run" command to run OCI_HelloWorld.java file which is under "src" folder.
I am getting below error after running "ant run" command.
C:\Project_Dir>ant run
Buildfile: C:\Project_Dir\build.xml
run:
[java] Error: Could not find or load main class OCI_HelloWorld
[java] Java Result: 1
BUILD SUCCESSFUL
Total time: 0 seconds
Below is my build.xml file
<!-- define the classpath for building the project -->
<path id="build.classpath">
<!-- include jar files under lib directory -->
<fileset dir="lib" includes="*.jar" />
</path>
<!-- define the classpath for running the project -->
<path id="run.classpath">
<!-- include jar files under lib directory -->
<fileset dir="lib" includes="*.jar" />
<!-- include the jar file resulting from building the project -->
<pathelement path="build/dist/OCI_HelloWorld.jar" />
</path>
<!-- build the jar file for the project -->
<target name="build" description="Build the HelloWorld program">
<!-- first compile your java class code -->
<mkdir dir="build/classes" />
<javac includeantruntime="false" srcdir="src" destdir="build/classes" debug="true" includes="C:\Project_Dir\src\*.java" fork="yes" executable="C:\Program Files\Java\jdk1.7.0_79\bin\javac">
<classpath refid="build.classpath" />
</javac>
<!-- now assemble a jar file from the compiled classes -->
<mkdir dir="build/dist" />
<jar jarfile="build/dist/OCI_HelloWorld.jar" fork="yes" basedir="build/classes">
<fileset dir="build/classes" includes="**/*.*" />
</jar>
</target>
<!-- run the project jar file -->
<target name="run" description="Run the HelloWorld program">
<!-- run java on the resulting code -->
<java classname="OCI_HelloWorld" fork="true" >
<classpath refid="run.classpath" />
</java>
</target>
<!-- reset build environment to starting point -->
<target name="clean" description="Cleans up your project build artifacts">
<delete dir="build" />
</target>
Please let me know what I need to do to get rid of this error.

Issue while creating war using ant with Jenkins

I have a ant build script which creates a war file. The file content are as follows.
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestProj" default="war" basedir=".">
<property name="project-name" value="${ant.project.name}" />
<property name="builder" value="IaasTeam" />
<property name="war-file-name" value="${project-name}.war" />
<property name="source-directory" value="src" />
<property name="classes-directory" value="build/classes" />
<property name="web-directory" value="WebContent" />
<property name="web-xml-file" value="WebContent/WEB-INF/web.xml" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="catalina.home" value="../../outside/project/lib"/>
<tstamp prefix="build-info">
<format property="current-date" pattern="d-MMMM-yyyy" locale="en" />
<format property="current-time" pattern="hh:mm:ss a z" locale="en" />
</tstamp>
<property name="build-directory" value="build" />
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
<fileset dir="${catalina.home}" includes="**/*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" classpathref="classpath" />
</target>
<target name="war" depends="clean,compile">
<mkdir dir="${build-directory}" />
<delete file="${build-directory}/${war-file-name}" />
<war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}">
<classes dir="${classes-directory}" />
<fileset dir="${web-directory}">
<!-- Need to exclude it since webxml is an attribute of the war tag above -->
<exclude name="WEB-INF/web.xml" />
</fileset>
<manifest>
<attribute name="Built-By" value="${builder}" />
<attribute name="Built-On" value="${build-info.current-date}" />
<attribute name="Built-At" value="${build-info.current-time}" />
</manifest>
</war>
</target>
I am using Jenkins as a build server (this is hosted on different machine kind of DEV environment).
I also use Gitlab as a repository and after pushing the latest code I have a hook for Jenkins job which gets triggered automatically and calls this build.xml.
Now the issues here is that when I run this script on my local machine everything works well but when Jenkins execute this it fails during the compilation phase giving me below error.
compile:
[mkdir] Created dir: /app/infra/jenkins/workspace/TestProj/build/classes
[javac] Compiling 49 source files to /app/infra/jenkins/workspace/TestProj/build/classes
BUILD FAILED
/app/infra/jenkins/workspace/TestProj/build.xml:27: /app/infra/jenkins/outside/project/lib does not exist.
The reason for this issue is the build server does not have any directoy called outside/project/lib.
The only reason of adding this directory in my build.xml is to have the container specific jar files ready for compiling.
How can I fix this issue?
Do I need to copy container specific jars on my build server? Or is there any way to tell Jenkins that not to copy this external jars but just use them for compilation.
Where would Jenkins find the jars? They need to be accessible otherwise your build will fail. If you don't want to have the files checked in (which is very sensible), you could use Apache Ivy to download them for you.
This is the most common way of handling the situation you're having. Using a dependency management framework like Ivy (or Maven, or similar) will save you a lot of headaches down the line. I recommend you have a look at their tutorial. After you set it up, your ant build will take care of downloading the files you need.

Selenium webdriver throws exception when trying to run via ANT

I am running my UI automation test cases using selenium via Eclipse, it goes through fine with no problem. Browsers are launched, test cases are executed, results are updated when I do it in Eclipse. Whereas, when I tried to run it via ANT, it started giving me exception,
java.lang.NoSuchMethodError: com.google.common.collect.Multimaps.transformValues(Lcom/google/common/collect/ListMultimap;Lcom/google/common/base/Function;)Lcom/google/common/collect/ListMultimap;
See the attached structure of my project,
My ANT file(build.xml) for reference,
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="uiAutomation" default="all" basedir=".">
<property name="example.dir" value="."/>
<property name="test.output" value="${example.dir}/build/test-output"/>
<property name="test.report" value="${example.dir}/build/test-report"/>
<property name="lib.dir" value="${example.dir}/lib/dependentJars/"/>
<target name="all" depends="prepare,compile,run" />
<!-- ==================================================================== -->
<!-- Compile Test Code -->
<!-- ==================================================================== -->
<path id="compile.cp">
<fileset dir="${lib.dir}" includes="*.jar" />
<fileset dir="${example.dir}/src/" includes="log4j.properties"/>
</path>
<target name="prepare">
<mkdir dir="${example.dir}/build/classes" />
<mkdir dir="${test.output}" />
<mkdir dir="${test.report}" />
</target>
<target name="compile" description="compile the test code" depends="prepare">
<echo message=" -- testng-compile-examples --" />
<javac includeantruntime="false" debug="true" fork="true" source="1.6" classpathref="compile.cp" srcdir="${example.dir}/src" destdir="${example.dir}/build/classes" />
</target>
<!-- ==================================================================== -->
<!-- Run Tests -->
<!-- ==================================================================== -->
<path id="run.cp">
<path refid="compile.cp" />
<pathelement location="${example.dir}" />
<pathelement location="${example.dir}/build/classes" />
</path>
<target name="run" depends="compile" description="Run examples using testng task with just -testclass and no xml">
<taskdef classpathref="run.cp" name="testng" classname="org.testng.TestNGAntTask" />
<echo>Starting tests...</echo>
<testng classpathref="run.cp" outputdir="${test.output}">
<!-- <classfileset dir="${example.dir}/build/classes/test" /> -->
<xmlfileset file="${example.dir}/src/testng.xml" />
<sysproperty key="org.uncommons.reportng.title" value="Fault Management Functional End to End Test" />
</testng>
<echo>Some of these tests should have failed, see the file test.out for the details</echo>
</target>
</project>
The surprise: Eclipse, understood automatically even if there are duplicate jars and was able to run the test scripts. However, ANT could not do that. So, I removed the jar com.google.common, which made me to run the test scripts from command line.
Lesson learnt: Do not have duplicate .jar files.
Even i got Similar issue with Maven build :
I have written a utility to generate PDF using Itext library for this i have used one of the available maven repository
<dependency>
<groupId>org.technbolts</groupId>
<artifactId>cucumber-contrib</artifactId>
<version>0.0.3</version>
</dependency>
Which was causing the above issue
Then i have used dependency
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.0.6</version>
</dependency>
Now its working correctly ... So Lesson learnt choose correct dependency .

using ANT to automatic run JUNIT cases, setting up dependencies

I have the following setup for my application:
The project TestAll currently contain a java file that run all the TestAll javafiles in all my other projects and this works as expected. The problem im facing is that I want this TestAll.java to be run from a ant script and have it record the result in a report file. This javafile is dependent on all the other projects in my application.
This is what I have so far:
<?xml version="1.0"?>
<project name="Ant-Test" default="main" basedir=".">
<!-- Sets variables which can later be used. -->
<property name="src.dir" location="" />
<property name="build.dir" location="../bin" />
<property name="dist.dir" location="../dist" />
<property name="lib.dir" location="../lib" />
<property name="test.dir" location="../src" />
<property name="test.report.dir" location="../testreport" />
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${dist.dir}" />
</target>
<!-- Define the classpath which includes the junit.jar and the classes after compiling-->
<path id="junit.class.path">
<pathelement location="${lib.dir}/junit.jar" />
<pathelement location="${build.dir}" />
</path>
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${dist.dir}" />
<mkdir dir="${test.report.dir}" />
</target>
<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="junit.class.path" />
</javac>
</target>
<target name="junit" depends="compile">
<junit printsummary="on" fork="true" haltonfailure="yes">
<classpath refid="junit.class.path" />
<formatter type="xml" />
<batchtest todir="${test.report.dir}">
<fileset dir="${src.dir}">
<include name="**/*Test*.java" />
</fileset>
</batchtest>
</junit>
</target>
<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
<jar destfile="${dist.dir}\se.testall.jar" basedir="${build.dir}">
<manifest>
<attribute name="Main-Class" value="test.Main" />
</manifest>
</jar>
</target>
<target name="main" depends="compile, jar, junit">
<description>Main target</description>
</target>
</project>
And the errors im getting is:
[javac] Compiling 1 source file to C:\Repositories\MyProject\TestAllProjects\bin
[javac] C:\Repositories\MyProject\TestAllProjects\src\se\testall\src\TestAllClass.java:6: error: package se.tlv.AProject.testall does not exist
[javac] import se.tlv.AnotherProject.testall.*;
[javac] ^
[javac] C:\Repositories\TestAllProjects\src\se\src\TestAllClass.java:7: error: package se.AnotherProject.testall does not exist
..and so on for all the internal imports in my TestAll project
This is most likley a classpath error where ANT in unable to find the files it needs, but I have no idea how to resolve it and have been trying for almost a full day. Any help is appreciated
The classpath provided to the javac task is: the junit jar, the build directory and the current directory.
Unless the current directory (where build.xml is located) is se, the javac task won't be able to find any java files to compile them.
Given that, the classpath for the javac task will need to include a path to each se directory in each project.
Edit:
Note: Unless you're planning on packaging the tests with the rest of the code, you should have two javac tasks that build to a build directory and a test build directory, then provide a path to each of those to junit so it can run the tests, but provide only the build directory path to the jar task.

Ant script in Eclipse uses the incorrect version of Java

I'm working on an older project that requires to use Java 1.5, but when I run the build script, I get the error:
java.lang.UnsupportedClassVersionError: Bad version number in .class file
I believe this error refers to a newer version of Java being used even though I only want to use Java 1.5. I've altered the settings under Windows->Preferences->Java->Installed JREs and under Run As->External Tool Configuration->JRE such that both are pointing to Java 1.5. Under Run As->External Tool Configuration->Environment, I added variables JAVA_HOME and JRE_HOME pointing to the older version of Java. When adding this line to the build script:
<echo message="ant.java.version: ${ant.java.version}"/>
Eclipse returns this value:
[echo] ant.java.version: 1.5
but the above error is still present. The only way I have been able to get the project to build is by altering my JAVA_HOME and JRE_HOME environment variables to point to the older versions of Java. Are there any other settings in Eclipse which I can change to build the project without changing my system environment variables? (i.e. Ant builds the project using Java 1.5 but the system variables still point at Java 1.7).
The code for build.xml:
<project name="osd-bits" basedir="." default="deploy">
<!-- Project settings -->
<!-- TODO -->
<!-- This needs to be set individually by each developer to the correct Tomcat path -->
<property name="tomcat.path" value="C:\Tomcat 6\apache-tomcat-6.0.35" />
<property name="project.title" value="OSD BITS"/>
<property name="project.name" value="osd-bits"/>
<property name="project.version" value="2.0"/>
<property name="project.deploypath" value="${tomcat.path}\\webapps"/>
<property name="project.workdir" value="${tomcat.path}\\work\Catalina\localhost"/>
<!-- Test Server Settings -->
<property name="project.deploypathtest" value="T:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirtest" value="T:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>
<!-- Staging Server Settings -->
<property name="project.deploypathstage" value="S:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirstage" value="S:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>
<!-- Production Server Settings -->
<property name="project.deploypathprod" value="P:\Program Files\jakarta-tomcat-5.0.28\webapps"/>
<property name="project.workdirprod" value="P:\Program Files\jakarta-tomcat-5.0.28\work\Catalina\localhost"/>
<!-- Path settings -->
<property name="path.binary" value="${basedir}/../../baja2/bin"/>
<property name="path.webroot" value="${basedir}/webroot"/>
<property name="path.conf" value="${basedir}/conf"/>
<property name="path.src" value="${basedir}/src"/>
<property name="path.build" value="${basedir}/bin"/>
<property name="path.cmdlib" value="${basedir}/cmd/lib"/>
<property name="path.buildlib" value="${tomcat.path}\\lib"/>
<property name="path.webinf" value="${path.webroot}/WEB-INF"/>
<property name="path.lib" value="${path.webinf}/lib"/>
<property name="path.classes" value="${path.webinf}/classes"/>
<property name="path.classpath" value="${path.lib}/baja.jar;
${path.lib}/struts.jar;
${path.buildlib}/fop.jar;
${basedir}/lib/servlet.jar;
${path.buildlib}/velocity-dep-1.4.jar;
${basedir}/lib/jxl.jar;
${path.lib}/display.jar;
${path.buildlib}/commons-validator.jar;
${path.buildlib}/commons-beanutils.jar;
${path.lib}/quartz-1.6.3.jar"/>
<!-- Check timestamp on files -->
<target name="timecheck">
<tstamp/>
</target>
<!-- Remove classes for clean build -->
<target name="clean" description="Prepare for clean build">
<delete dir="${path.build}/com"/>
<delete file="${path.lib}/${project.name}.jar"/>
<delete file="${path.lib}/${project.name}-config.jar"/>
</target>
<!-- jar up config files -->
<target name="config">
<jar jarfile="${path.lib}/${project.name}-config.jar" basedir="${path.conf}"/>
</target>
<!-- Full build of java and config files-->
<target name="build" depends="timecheck,clean,config">
<echo message="ant.java.version: ${ant.java.version}" />
<javac srcdir="${path.src}" destdir="${path.build}" classpath="${path.classpath}" debug="true" target="1.5" includeantruntime="false"/>
<jar jarfile="${path.lib}/${project.name}.jar" basedir="${path.build}"/>
</target>
<!-- Builds everything in src directory and deploys .war to deployment directory -->
<target name="deploy" depends="build" description="Deploys project to deployment directory">
<delete file="${project.deploypath}\${project.name}.war"/>
<delete dir="${project.deploypath}\${project.name}"/>
<delete dir="${project.workdir}\${project.name}"/>
<copy todir="${project.deploypath}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<!-- Builds everything in src directory and deploys to test server deployment directory -->
<target name="deploytotest" depends="build" description="Deploys project to test deployment directory">
<delete file="${project.deploypathtest}\${project.name}.war"/>
<delete dir="${project.deploypathtest}\${project.name}"/>
<delete dir="${project.workdirtest}\${project.name}"/>
<copy todir="${project.deploypathtest}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<!-- Builds everything in src directory and deploys to staging server deployment directory -->
<target name="deploytostage" depends="build" description="Deploys project to stage deployment directory">
<delete file="${project.deploypathstage}\${project.name}.war"/>
<delete dir="${project.deploypathstage}\${project.name}"/>
<delete dir="${project.workdirstage}\${project.name}"/>
<copy todir="${project.deploypathstage}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<!-- Builds everything in src directory and deploys to production server deployment directory -->
<target name="deploytoprod" depends="build" description="Deploys project to production deployment directory">
<delete file="${project.deploypathprod}\${project.name}.war"/>
<delete dir="${project.deploypathprod}\${project.name}"/>
<delete dir="${project.workdirprod}\${project.name}"/>
<copy todir="${project.deploypathprod}\${project.name}">
<fileset dir="${path.webroot}"/>
</copy>
</target>
<target name="movejsps" description="Moves all JSP files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\jsp" preservelastmodified="false" >
<fileset dir="${path.webroot}/jsp" />
</copy>
</target>
<target name="movejs" description="Moves all JS files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\js" preservelastmodified="false" >
<fileset dir="${path.webroot}/js" />
</copy>
</target>
<target name="movecss" description="Moves all CSS files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\css" preservelastmodified="false" >
<fileset dir="${path.webroot}/css" />
</copy>
</target>
<target name="movetemplates" description="Moves all Report Template files without doing a complete rebuild">
<copy todir="${project.deploypath}\${project.name}\templates" preservelastmodified="false" >
<fileset dir="${path.webroot}/templates" />
</copy>
</target>
<target name="copyjar" description="Moves latests baja.jar into demos webinf lib">
<copy file="${path.binary}/baja.jar" todir="${path.lib}" preservelastmodified="true" />
</target>
<!-- Builds the command line distribution -->
<target name="deploycmd" depends="build" description="Deploys project to test deployment directory">
<delete file="${path.cmdlib}\${project.name}.jar"/>
<delete file="${path.cmdlib}\${project.name}-config.jar"/>
<copy file="${path.lib}/${project.name}.jar" todir="${path.cmdlib}" preservelastmodified="true" />
<copy file="${path.lib}/${project.name}-config.jar" todir="${path.cmdlib}" preservelastmodified="true" />
</target>
There might be multiple causes:
you use a library that was compiled for java 1.6
inside the ant script, you can specify the java version you want to compile to. It might be a different version, than the version, that ant itself runs in.
inside the eclipse settings, you can select an ant installation. The ant installation might be compiled for java 1.6 (not very propable). You might add(?)/select another ant installation
if ant does not compile your source but uses the class files, that the eclipse autocompilation already produced, you need to change the project's java compiler version and the projects JRE in the project settings (obviously there ;-)
Go to the library with all the jars. If you can run a bash script, do this:
#!/bin/bash
for jarfile in `ls *.jar`
do
directory=${jarfile%.jar}
if [ -d $directory ]
then
rm -r $directory
fi
mkdir $directory
cd $directory
jar xf ../$jarfile
cd ..
for classfile in `find ./ -name *.class`
do
file $classfile | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"
done
done
That should spit out any class that is not version 46 (Java 1.2), version 47 (Java 1.3), 48 (Java 1.4) or 49 (Java 1.5). The quartz jar has a class that's 45.3, which is Java 1.1. Maybe that's an issue? If not (Googling suggests JVM 1.5 can run 1.1 classes), then exclude "45.3" as well:
file $classfile | grep -v "version 45.3" | grep -v "version 46" | grep -v "version 47" | grep -v "version 48" | grep -v "version 49"
If you are not using eclipse. Then you can change the ant java property directly on the file as mentioned here. Works for me.
http://pissedoff-techie.blogspot.in/2014/09/ant-picks-up-incorrect-java-version.html

Categories