Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassLoader getResourceAsStream returns null

My project directory structure (in Eclipse):

MyProject/     src/        --> "source directory" on Eclipse's classpath/buildpath         com.me.myapp             Driver             myconfig.txt 

In Driver, I have the following code:

public class Driver {     public static void main(String[] args) {         InputStream is = ClassLoader.getSystemClassLoader.getResourceAsStream("myconfig.txt");         if(is == null)             System.out.println("input stream is null");         else             System.out.println("input stream is NOT null :-)");     } } 

When I run this I get the following console output:

input stream is null 

Why? Have I placed myconfig.txt in an incorrect location? Am I using the ClassLoader API incorrectly? Something else?

like image 751
IAmYourFaja Avatar asked Sep 26 '13 18:09

IAmYourFaja


People also ask

What is getResourceAsStream in Java?

The getResourceAsStream method returns an InputStream for the specified resource or null if it does not find the resource. The getResource method finds a resource with the specified name. It returns a URL to the resource or null if it does not find the resource. Calling java. net.

Should I close getResourceAsStream?

You should always close streams (and any other Closeable, actually), no matter how they were given to you.

When using class getResourceAsStream Where will the resource be searched?

The java. lang. Class. getResourceAsStream() finds a resource with a given name.It returns a InputStream object or null if no resource with this name is found.


2 Answers

If it's in the same package use

InputStream is = Driver.class.getResourceAsStream("myconfig.txt"); 

The way you have it

InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("myconfig.txt"); 

It's looking for the file in the root of the classpath. You could use

InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("com/me/myapp/myconfig.txt"); 

The rules for searching are explained in the javadoc of ClassLoader#getResource(String) and the javadoc of Class#getResource(String).

like image 98
Sotirios Delimanolis Avatar answered Sep 29 '22 15:09

Sotirios Delimanolis


If you are working with Maven, add the following lines under BUILD tag. You get this error when you are running the webapp on server but there is no reference to the resources on the server.

So, add this the following into your POM.xml and see the magic.

<resources>     <resource>         <directory>src/main/resources</directory>         <filtering>true</filtering>         <includes>             <include>fileName.txt</include>         </includes>     </resource>     <resource>         <directory>src/main/resources</directory>         <filtering>true</filtering>         <includes>             <include>fileName.wsdl</include>         </includes>     </resource> </resources> 

Have Fun.!!!

like image 27
Hitesh Avatar answered Sep 29 '22 15:09

Hitesh