Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

getClass().getResource and getClass().getClassLoader().getResource return Null

Salman5Jan 21 2014 — edited Jan 21 2014

I want to use some image in my application. But, I do not want to hard code the image path because where ever I stored the application it should works. I tried to use getClass().getResource and getClass().getClassLoader().getResource but in both cases I am getting NULL

.

here is the actual path for the image:

C:\JDeveloper\mywork\MyApplication\ViewController\public_html\resources\images\img.gif

and here is the actual path for the class I am working with:

C:\JDeveloper\mywork\MyApplication\ViewController\src\view\MyClassBean

So both the class and the image in the same project ViewController

here is the output statements from MyClassBean Class:

System.out.println("1. "+  getClass());
  
System.out.println("2. "+  getClass().getClassLoader());
  
System.out.println("----------------------------------------------------------------------------------------------------------------------------");
  
System.out.println("1. "+  getClass().getClassLoader().getResource("img.gif") );
  
System.out.println("2. "+  getClass().getClassLoader().getResource("/img.gif") );
  
System.out.println("3. "+  getClass().getClassLoader().getResource("images/img.gif") );
  
System.out.println("4. "+  getClass().getClassLoader().getResource("/images/img.gif") );
  
System.out.println("5. "+  getClass().getClassLoader().getResource("resources/images/img.gif") );
  
System.out.println("6. "+  getClass().getClassLoader().getResource("/resources/images/img.gif") );
  
System.out.println("7. "+  getClass().getClassLoader().getResource("public_html/resources/images/img.gif") );
  
System.out.println("8. "+  getClass().getClassLoader().getResource("/public_html/resources/images/img.gif") );
  
System.out.println("9. "+  getClass().getClassLoader().getResource("ViewController/public_html/resources/images/img.gif") );
  
System.out.println("10. "+ getClass().getClassLoader().getResource("/ViewController/public_html/resources/images/img.gif") );
  
System.out.println("11. "+ getClass().getClassLoader().getResource("myApplication/ViewController/public_html/resources/images/img.gif") );
  
System.out.println("12. "+ getClass().getClassLoader().getResource("/myApplication/ViewController/public_html/resources/images/img.gif") );
  
System.out.println("----------------------------------------------------------------------------------------------------------------------------");
  
System.out.println("1. "+  getClass().getResource("img.gif") );
  
System.out.println("2. "+  getClass().getResource("/img.gif") );
  
System.out.println("3. "+  getClass().getResource("images/img.gif") );
  
System.out.println("4. "+  getClass().getResource("/images/img.gif") );
  
System.out.println("5. "+  getClass().getResource("resources/images/img.gif") );
  
System.out.println("6. "+  getClass().getResource("/resources/images/img.gif") );
  
System.out.println("7. "+  getClass().getResource("public_html/resources/images/img.gif") );
  
System.out.println("8. "+  getClass().getResource("/public_html/resources/images/img.gif") );
  
System.out.println("9. "+  getClass().getResource("ViewController/public_html/resources/images/img.gif") );
  
System.out.println("10. "+ getClass().getResource("/ViewController/public_html/resources/images/img.gif") );
  
System.out.println("11. "+ getClass().getResource("myApplication/ViewController/public_html/resources/images/img.gif") );
  
System.out.println("12. "+ getClass().getResource("/myApplication/ViewController/public_html/resources/images/img.gif") );

here is the output Results:



1. class view.MyClassBean
2. weblogic.utils.classloaders.ChangeAwareClassLoader@16a1d36 finder: weblogic.utils.classloaders.CodeGenClassFinder@a94081 annotation: MyApplication@MyApplication-ViewController-context-root
----------------------------------------------------------------------------------------------------------------------------
1.  null
2.  null
3.  null
4.  null
5.  null
6.  null
7.  null
8.  null
9.  null
10. null
11. null
12. null
----------------------------------------------------------------------------------------------------------------------------
1.  null
2.  null
3.  null
4.  null
5.  null
6.  null
7.  null
8.  null
9.  null
10. null
11. null
12. null

So, How Can I get any image that stored in the same project where the running class is with in the java code. I am using JDeveloper 11g Release 2 with ADF technology.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 18 2014
Added on Jan 21 2014
1 comment
691 views