1

Is there any way to add resources to a class or package in java? For example, if I use JFileChooserto give user an option to choose any image file. Let's say user has already selected a file from his desktop(or any other directory). I know that I can get it's location by calling method getSelectedFile().toString() and it will return me directory of file in String format.

So now my desired operation is that I want to add this file into the resources of class. I want to add because I want to retrieve this image/file later by calling this.class.getResource() method.

Please guide me, Thanks.

Junejo
  • 717
  • 1
  • 7
  • 14
  • Why are you retrieving user-provided files (which are guaranteed NOT to be part of your class resources) in such a way? Why aren't you using the regular filesystem IO API? – Thorn G Aug 05 '13 at 20:44
  • As @TomG said why don't you store selected file into some other directory and read it from there with normal java IO? – Piro Aug 06 '13 at 10:04

1 Answers1

1

In the following link is a rather ugly hack that adds to the SystemClassLoader at runtime. Adding files to java classpath at runtime

Community
  • 1
  • 1
Khinsu
  • 1,487
  • 11
  • 27
  • Is there not any simple way? My requirement is just that I want to add a file into resources at run-time. For example:- `String fileName = "C:/Users/Zulfiqar Junejo/Desktop/profile.jpg"` Now is there any way to add the file represented by this dir to resources? – Junejo Aug 05 '13 at 18:08
  • You can only add folders or jar files to a class loader(which contains all resources). See Javadoc: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String) – Khinsu Aug 05 '13 at 18:13
  • [How to Answer](http://stackoverflow.com/questions/how-to-answer): **Provide context for links** _A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there . Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline._ – BackSlash Aug 05 '13 at 18:13