90

In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sources folder), how do I do it?

Veera
  • 32,532
  • 36
  • 98
  • 137

7 Answers7

114

ClassLoader.getResourceAsStream().

As stated in the comment below, if you are in a multi-ClassLoader environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader(). See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388.

charleslparker
  • 1,904
  • 1
  • 21
  • 31
cletus
  • 616,129
  • 168
  • 910
  • 942
  • See personal post for a code example: http://tshikatshikaaa.blogspot.nl/2012/07/maven-how-to-access-filesdata-in.html – Jérôme Verstrynge Aug 07 '12 at 19:36
  • 14
    If you are in a multi classloader environment (such as unit testing/ webapps etc), you may need to use this Thread.currentThread().getContextClassLoader(). See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388 – khylo Feb 28 '13 at 14:42
  • Please add @khylo's suggestion to your answer! – froginvasion Jan 09 '15 at 12:43
  • 10
    Another way: `InputStream is = new ClassPathResource("/path/to/your/file").getInputStream()` – zhuguowei Mar 08 '18 at 11:14
  • 2
    @zhuguowei ClassPathResource is a Spring class. – ichalos Jul 07 '18 at 11:03
34
ClassLoader.class.getResourceAsStream("/path/file.ext");
thkala
  • 84,049
  • 23
  • 157
  • 201
jkarretero
  • 451
  • 4
  • 4
  • but if deploy web app in tomcat this way will get error:`java.lang.NullPointerException: null `, and the simplest way I think is `new ClassPathResource("/path/to/your/file").getInputStream()` – zhuguowei Mar 08 '18 at 11:12
  • 1
    can you please tell how o make it available in war – Vikram Saini Aug 30 '18 at 16:39
  • I got the same problem when i deploy. Can anyone found a solution to this situation? – Augusto Sep 06 '18 at 16:32
13

That depends on where exactly the XML file is. Is it in the sources folder (in the "default package" or the "root") or in the same folder as the class?

In for former case, you must use "/file.xml" (note the leading slash) to find the file and it doesn't matter which class you use to try to locate it.

If the XML file is next to some class, SomeClass.class.getResourceAsStream() with just the filename is the way to go.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
11

ClassLoader.class.getResourceAsStream("/path/to/your/xml") and make sure that your compile script is copying the xml file to where in your CLASSPATH.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
Clint
  • 8,988
  • 1
  • 26
  • 40
6

someClassWithinYourSourceDir.getClass().getResourceAsStream();

mP.
  • 18,002
  • 10
  • 71
  • 105
4

Some of the "getResourceAsStream()" options in this answer didn't work for me, but this one did:

SomeClassWithinYourSourceDir.class.getClassLoader().getResourceAsStream("yourResource");

user64141
  • 5,141
  • 4
  • 37
  • 34
0

I tried proposed solution and forward slash in the file name did not work for me, example: ...().getResourceAsStream("/my.properties"); null was returned

Removing the slash worked: ....getResourceAsStream("my.properties");

Here is from doc API: Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:

    modified_package_name/name 

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e'). 
Striker
  • 331
  • 3
  • 6
  • In my case, I was getting `null` **without** the `/`. Adding the slash character worked for me. There must be some other difference between @hussein-terek and my setup and your setup. – Ajoy Bhatia Jun 01 '18 at 20:13