1
InputStream str = this.getClass()
                      .getClassLoader() 
                      .getResourceAsStream( "filename" );

The file is in the same package as the class.
It is returning null every time. Is there anyway we can replace getResourceAsStream(...) with some other method?

Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
user1855888
  • 131
  • 2
  • 5

1 Answers1

2

Don't use getClassLoader if you want to load things from the same package. If you do that, you need to give the full path.

Just

 InputStream str = this.getClass().getResourceAsStream( "filename" );
Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • This is also not working. I tried it. Do I need to put any backslash (/) anywhere? And I've heard that the directory path will not fetch the results but the classspath will? What is that? – user1855888 Nov 27 '12 at 09:06
  • No, if the file is really in the same package, that would have worked. Where are the files exactly? the class file (NOT the source file), and the file you want to load? Both in the same JAR file? – Thilo Nov 27 '12 at 09:07
  • The file we want to retrieve is an ovf file. Not a java file. And it is in the src folder. The .class is in the classes folder. – user1855888 Nov 27 '12 at 09:09
  • 1
    You need to copy the ovf file to the classes folder (if that is were you load classes from). – Thilo Nov 27 '12 at 09:26