I’m sorry this is a newbie question, but i’m having some problems with Ant, and i’m not really sure about how to solve them.
I don’t know how to add a folder containing png and jpg images. Those images will be later be displayed, and will be called using the command:
new URL("file:///" + System.getProperty("user.dir") + "\\img\\IMAGENAMEHERE.png")
The files are in the folder /img in the folder next to /src.
So, i have my ant build file, and my classpath is:
<path id="classpath">
<fileset dir="libs">
<include name="**/*.jar" />
</fileset>
<pathelement location="${build}" />
<pathelement location="${instrumented.dir}" />
<pathelement location="${build}/tests" />
<pathelement location="${libs}" />
<pathelement location="${img}" />
</path>
Since that didn’t work, i tried copying the images into the build folder (in the ant build file, i create a build folder and a bin folder)
<target name="setup">
<delete dir="${build}" />
<mkdir dir="${build}"/>
<mkdir dir="${build}/app"/>
<mkdir dir="${build}/tests"/>
<copy todir="${build}/img">
<fileset dir="${img}" >
<include name="**/*.png" />
<include name="**/*.jpg" />
</fileset>
</copy>
</target>
But that didn’t work either, and i don’t know what to do. The problem is, whenafter i compile with Ant, i got this error when i run it in the console calling java StartProgram :
Unable to obtain resource from C:\folder\img\salidaCerrada.png: java.util.zip.ZipException: error in opening z
ip file
[junit] Unable to obtain resource from C:\folder\img\salidaCerrada.png:
[junit] java.util.zip.ZipException: error in opening zip file
[junit] at java.util.zip.ZipFile.open(Native Method)
[junit] at java.util.zip.ZipFile.<init>(ZipFile.java:127)
[junit] at java.util.jar.JarFile.<init>(JarFile.java:135)
[junit] at java.util.jar.JarFile.<init>(JarFile.java:99)
[junit] at org.apache.tools.ant.AntClassLoader.getResourceURL(AntClassLo
ader.java:1006)
Any idea? Thank you beforehand.
Edit: I forgot to mention that i’m not creating a .jar file, but just testing my application and compiling it (creating .class files). The images aren’t in a .jar file either.
My first thought is that your ${img} element isn’t pointing to anywhere. If you used a tool to create your build.xml file then it probably created the ${libs} and other properties. First you should replace the ${img} with just ‘img’ (no quotes).
My second thought is that you are looking for the images in the “working” directory, but that may not be set to what you think it should be, especially if your running inside some other tool. I suggest you print out the value of System.getProperty(“user.dir”) to see what it actually is. Depending on what you need to do, you will probably get better results if you load the images from the class path (you should be able to find some good tutorials on how to load a Resource Stream from the class path).