I got a TestNG test, that works perfectly fine on it's own. Somewhere in it I got:
URL fileName = this.getClass().getClassLoader().getResource("config.properties");
I'm trying to run the same test using Ant:
<testng outputDir="${resultsFolder}" haltOnFailure="false">
<classpath>
<fileset dir="./src/">
<include name="**/*.properties"/>
</fileset>
-----------------------------...--------------------
</classpath>
<classfileset dir="correct path here" includes="**/*.class" />
</testng>
I can see in debug mode that the config.properties is in the classpath. But the line at the top can not find it, it is null.
EDIT: I solved it.The critical line actually does not search for the file in the classpath directly, it searches IN the files/folders. So, this solved my problem:
<pathelement location="src/"/>
Thanks for the help.
try to replace your <classpath>...</classpath> with this:
<classpath>
<pathelement path="./src"/>
</classpath>
In order for JVM to find the config.properties the parent directory of config.properties should be in classpath.
Related
I am new to JUnit and ant. I doubt whether my build.xml is correct. I am able to generate .class file through compile tag in target. But I am not able to execute the file. When I try to execute the test tag in the below script. I got java.lang.ClassNotFoundException, this is the where I got stuck.
Same program works in eclipse IDE fine.. But not able to execute through ant only
I have verified classpath for java and ant multiple times. It is fine too
I am trying to sort this out from past three days..But still problem persists..
Someone please help me
<target name="compile" description = "Compiling java code">
<javac srcdir="D:/AntBuilder/src" destdir="D:/AntBuilder/build" classpath= "D:/AntBuilder/dist/lib" includeantruntime="true" />
</target>
<target name="test" depends="compile" description="Execute Unit Tests" >
<junit printsummary="yes" fork="yes" haltonfailure="yes">
<formatter type="xml" />
<test name="automation.LogInTest" />
<classpath>
<pathelement location="D:/AntBuilder/dist/lib"/>
<pathelement location="D:/AntBuilder/build"/>
</classpath>
</junit>
</target>
Vamshi G
As per the stacktrace, it is not able to locate WEbDriver.exe. Download the file and place it where all jars are located.
Sorted out the issue now.
Problem lies with adding jar files in lib folder. Along with selenium-java-2.41.0.jar file we need to download one more jar file.. selenium-server-standalone-2.41.0.jar
Since last update of ADT & build tools, I cannot build my application via the ant release request.
The ant output shows multiple "cannot find symbol" errors when referencing a class file located in a subfolder of libs/.
I tried modifying ant's build.xml as following :
<path id="java.compiler.classpath">
<fileset dir="${jar.libs.dir}">
<include name="**/*.jar **/*.class" />
</fileset>
</path>
But it doesn't work.
Compiling via Eclipse is fine.
I am truly not an ant expert, but is there something obvious I am missing ?
EDIT :
I tried updating ant and it changes nothing.
I also tried replacing the code above by the following, without success :
<path id="java.compiler.classpath">
<fileset dir="${jar.libs.dir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${jar.libs.dir}" />
</path>
Any ideas ?
http://ant.apache.org/manual/Tasks/ant.html
Try this, you might be able to find the solution due to the update.
If not that..maybe this one..
http://ant.apache.org/manual/Tasks/java.html
I don't think your include is correct. I think you need to do it like this:
<include name="**/*.jar" />
<include name="**/*.class" />
Here is a way I've done it previously:
<fileset dir = "${lib}" includes = "**/*.jar"/>
<fileset dir = "${webLib}" includes = "**/*.jar"/>
When trying to perform an ant build, I receive a message package org.junit does not exist. However, in build.xml, I have:
<junit>
<classpath>
... some stuff ...
<fileset dir="dependlibs" includes="**/*.jar" />
</classpath>
... other stuff ...
</junit>
Now, in the same directory in which I am trying to run ant (the directory with build.xml), I have an org directory which contains junit.jar. Can Anyone point Me in the direction of information to show Me what I am doing wrong?
The problem is that junit.jar is not in ants classpath. Provided your basedir is correct, you should be able to add it to the classpath by adding:
<junit>
<classpath>
<fileset dir="org">
<include name="**/*.jar" />
</fileset>
....
</classpath>
</junit>
I have an android application in java. I want to test it using a xml script with ant. Now we have several packages and a standard directory structure for our source and test files.
There is a "tests" folder (root of all test code) which has 1 file say A.java and some sub-folders which again in-turn contain some test files or sub-sub-folders. Only file A.java gets tested and rest others are not tested. If i change the directory path in xml script from "tests" to say "tests/ui" then test files in folder "ui" are not able to test and i get some error - Class Not Found Error when i check out the files in "test-result" folder where i store the output of the test.
I have included the parent directory of compiled codes and required external jars in path with id = "test.classpath". I think i should every file of type .class rather then path to parent folder.
The code is :
<target name="test-run" description="Run Test">
<delete dir = "test_result" />
<mkdir dir = "test_result" />
<junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
<classpath refid="test.classpath"/>
<batchtest fork="yes" todir="test_result">
<formatter type="xml"/>
<fileset dir="tests" includes="**/*" />
</batchtest>
</junit>
</target>
Can you please explain me how to achieve my motive assuming a generic file structure.
EDIT :
Following is the path used in above xml where folder out contains .class files of source and test files generated by idea ide :
<path id="test.classpath">
<pathelement path="testlibs/jmockit.jar"/>
<pathelement path="testlibs/jmockit-coverage.jar"/>
<pathelement path="testlibs/jmockit-coverage-htmlfull.jar"/>
<pathelement path="/opt/idea-IU-117.418/lib/junit-4.10.jar"/>
<pathelement path="out"/>
</path>
Try changing the filesetelement to this:
<fileset dir="tests" includes="**/*.java" />
I created my first Ant script and it's working pretty well. It compiles all my java sources, creates a .jar file and runs the program without even any warning.
But when i try to run my .jar from command line i get NoClassDefFoundError exceptions.
So, how to translate this Ant snippet to work from command line?
<property name="main.class" value="de.bfs.radon.omsimulation.OMMainFrame"/>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${bin.dir}/omsimulation-${version}.jar"/>
</classpath>
</java>
</target>
This is the command line:
# java -classpath lib/ -jar bin/omsimulation-0.4.45-beta3.jar
Throws:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/toedter/calendar/JDateChooser
at de.bfs.radon.omsimulation.OMMainFrame.(OMMainFrame.java:133)
at de.bfs.radon.omsimulation.OMMainFrame$1.run(OMMainFrame.java:106)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
Why does my .jar file not work with the defined classpath? Further down the Ant script:
<target name="jar" depends="manifest">
<mkdir dir="${bin.dir}" />
<jar jarfile="${bin.dir}/omsimulation-${version}.jar" manifest="${src.dir}/omsimulation.manifest" compress="no" basedir="${build.dir}" includes="de/**" />
</target>
<target name="manifest" depends="compile">
<manifestclasspath property="manifest.cp" jarfile="${bin.dir}/omsimulation-${version}.jar">
<classpath refid="classpath" />
</manifestclasspath>
<manifest file="${src.dir}/omsimulation.manifest">
<attribute name="Built-By" value="${author}"/>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</target>
Again, running the Ant script works fine. I even tried adding the said libraries to my .jar but that only blows up the file size, the problem still persists.
<jar jarfile="${bin.dir}/omsimulation-${version}.jar" manifest="${src.dir}/omsimulation.manifest" compress="no" basedir="${build.dir}"> <!-- includes="de/**" /-->
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</jar>
Any ideas on this?
Thanks a lot,
donc_oe
SOLVED: Thanks to perception, the unix command line i was looking for is:
# java -cp lib/*:bin/myjarfile.jar my.package.MyMainClass
And for Windows (note the ; semicolon):
# javaw -cp lib/*;bin/myjarfile.jar my.package.MyMainClass
The relevant thing to note from your build script is this:
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
With that little snippet you have defined a path construct in Ant, which you then refer to in your run task:
<target name="run" depends="jar">
<java fork="true" classname="${main.class}">
<classpath>
<path refid="classpath"/>
<path location="${bin.dir}/omsimulation-${version}.jar"/>
</classpath>
</java>
</target>
This is in effect executing:
java -cp ${lib.dir}/*.jar:${bin.dir}/omsimulation-${version}.jar ${main.class}
Of course, it does so without the squigly lines and the path(s) fully substituted. The main point being that the command you are trying to run yourself is not equivalent at all. When attempting to invoke from the command line you will need to include in the classpath all the necessar JAR's containing your code and all third party libraries. Assuming everything is still bundled in the Ant created folders, something like:
java -cp <full-path-to-lib>/* -jar <full-path-to-bin>/omsimulation-0.4.45-beta3.jar
Or:
java -cp <full-path-to-lib?/*:<full-path-to-bin>/omsimulation-0.4.45-beta3.jar <MainClass>
ClassDefNotFoundException most likely occur when class is found in classpath, but it is loaded in different classloader, or in a wrong path etc.
From the build file, you appears to create jar that include other jars. This may not give a result you want.
Most likely you want a solution described in Easiest way to merge a release into one JAR file. My personal favorite is one-jar.