I may have made of try, none works..
The file is:
/Users/Toto/Desktop/Titi/IUT/Java/TP2/project/src/fichierPointJava/img1.png
fichierPointJava
is the name of the package .
I launch the ant when I am situated in project which contains the build.xml
Here are the codes that I tested:
URL urlImage1=this.getClass().getClassLoader.getResource("/src/fichierPointJava/img1.png");
URL urlImage1=this.getClass().getClassLoader.getResource("/fichierPointJava/img1.png");
URL urlImage1=this.getClass().getClassLoader.getResource("fichierPointJava/img1.png");
URL urlImage1=this.getClass().getClassLoader.getResource("/img1.png");
URL urlImage1=this.getClass().getClassLoader.getResource("img1.png");
System.out.println("Value = "+ urlImage1);
I made out a will with or without this
, and with or without getClassLoader()
Hope someone will help me.
Thanks you
If you have the following package layout
then the the following should work
When using the
ClassLoader
you need to pass the full qualified name of the resource, i.e including the package name.When using the
Class
then the path - if not starting with/
- is relative to the package where the class which is trying to load the resource is located, otherwise it is the absolute name of the resource.You can read more about ClassLoader#getResource and Class#getResource in the javadocs.
Make sure that the ant target you are running to create the jar include the
*.png
resources. You can verify this by opening the jar with the zip-tool of your choice. The directorysrc
should not be included.