ant script, problem with classpath

H

harryos

I am writing some code that uses some classes from jai.
i have the following dir structure for code i am trying to compile

-src ----contains my src code(mypackage.MyCode.java)
-lib----contains jai_codec.jar,jai_core.jar
build.xml

I have set the classpath as below,and wrote targets
.....
<property name="main-class" value="mypackage.MyClass"/>
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
....

<target name="compile" depends="init">
<javac srcdir="src" destdir="${classes.dir}"
classpathref="classpath"/>
</target>

<target name="jar" depends="compile">
<jar destfile="${dist.dir}/mycode.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>

<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${dist.dir}/mycode.jar"/>
</classpath>
</java>
</target>
.....

when i try 'ant run' from the cmd prompt i get a set of errors
starting with
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/
jai/OperationRegistrySpi ....etc

I am able to compile the code by 'ant compile' successfully..
'ant jar' too works and puts the jar file in folder 'dist'.

Is there something wrong with the ant script? Is there some way i can
echo the classpath from inside the run target sothat i can know the
runtime classpath?
can someone help?

thanks
harry
 
J

John B. Matthews

[...]
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
[...]

Try this:

<path id="classpath">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<echo>classpath: ${toString:classpath}</echo>
 
L

Lew

John said:
[...]
<path id="classpath">
<fileset dir="lib" includes="**/*.jar"/>
</path>
[...]

Try this:

<path id="classpath">
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
</path>
<echo>classpath: ${toString:classpath}</echo>

Also, harryos, notice how John gave moderate indentation, only 4 spaces, to
his posted code, thus making his post much more readable than if he'd used,
say, 14 spaces or two TABs and a space.
 
H

harryos

thanks for the replies
i tried ant run with -v and it printed the classpath including the
jars in 'lib' directory,and the jar created in 'dist' directory
here is what ant run -v returns
run:
[java] Executing 'E:\Java\jdk1.6.0_05\jre\bin\java.exe' with
arguments:
[java] '-classpath'
[java] 'F:\my\code\java\lib\jai_codec.jar;F:\my\code\java\lib
\jai_core.jar;F:\my\code\java\dist\mycode.jar'
[java] 'mypackage.MyCode'

However,the same error staring with

'[java] Exception in thread "main"java.lang.NoClassDefFoundError:
javax/media/jai/OperationRegistrySpi
[java]at java.lang.ClassLoader.defineClass1(Native Method)
[java]at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
[java]at java.security.SecureClassLoader.defineClass(SecureClassLoader
..java:124)...
occurs..

finally i tried to run java from cmd line,using the same classpath (as
printed by -v option)
java -classpath F:\my\code\java\lib\jai_codec.jar;F:\my\code\java\lib
\jai_core.jar;F:\my\code\java\dist\mycode.jar mypackage.MyCode

this works and there is no error message!
I don't understand why ant run causes the problem whereas java from
cmd line works when both use the same classpath
Can someone pls help..?

harry
 
J

John B. Matthews

harryos said:
thanks for the replies
i tried ant run with -v and it printed the classpath including the
jars in 'lib' directory,and the jar created in 'dist' directory
here is what ant run -v returns
run:
[java] Executing 'E:\Java\jdk1.6.0_05\jre\bin\java.exe' with
arguments:
[java] '-classpath'
[java] 'F:\my\code\java\lib\jai_codec.jar;F:\my\code\java\lib
\jai_core.jar;F:\my\code\java\dist\mycode.jar'

I'm surprised to see no ant library jars in this list.
However,the same error staring with

'[java] Exception in thread "main"java.lang.NoClassDefFoundError:
javax/media/jai/OperationRegistrySpi
[java]at java.lang.ClassLoader.defineClass1(Native Method)
[java]at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
[java]at java.security.SecureClassLoader.defineClass(SecureClassLoader
.java:124)...

Does javax/media/jai/OperationRegistrySpi.class exist?
finally i tried to run java from cmd line,using the same classpath (as
printed by -v option)
java -classpath F:\my\code\java\lib\jai_codec.jar;F:\my\code\java\lib
\jai_core.jar;F:\my\code\java\dist\mycode.jar mypackage.MyCode

this works and there is no error message!
I don't understand why ant run causes the problem whereas java from
cmd line works when both use the same classpath
Can someone pls help..?

You might verify file names and encoding in the jar. I recently found a
jar with an invalid CEN header in the manifest; I had to unpack and
repackage it. Also, look at the ant FAQ:

<http://ant.apache.org/faq.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top