Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
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? –  Tom G Aug 5 '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 6 '13 at 10:04

1 Answer 1

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

share|improve this answer
    
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? –  Zulfiqar Junejo Aug 5 '13 at 18:08
    
You can only add folders or jar files to a class loader(which contains all resources). See Javadoc: docs.oracle.com/javase/7/docs/api/java/lang/… –  Khinsu Aug 5 '13 at 18:13
    
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 5 '13 at 18:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.