• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 554
  • Last Modified:

getResourceAsStream is returning null

Hello All,

I am facing problem with getResourceAsStream. When I have tested this with following test application also, it is raising problem.

When I bundle the class into jar and trying to run then it is returning null. otherwise it is running perfectly

What is the mistake  I am doing here.

public class Test{
      /**
       *
       * @param arguments
       * @throws Exception
       */
      public static void main(String arguments[])throws Exception
      {
            Test test=new Test();
            InputStream in =test.getClass().getResourceAsStream("a.txt");
            if(in==null)
            {
                  System.err.println("Error Raised here");
            }
            while(true && in!=null)
            {
                  int c=in.read();
                  if(c==-1 || c>=255)
                        break;
                  System.err.print((char)c);
            }
            
      }

}
0
sudhakar_koundinya
Asked:
sudhakar_koundinya
  • 6
  • 3
  • 3
1 Solution
 
girionisCommented:
What happens if you do this:

URL url = getClass().getResource("a.txt");
InputStream is = url.openStream();
0
 
sudhakar_koundinyaAuthor Commented:
even I have tried this also

object.getClass().getResourceAsStream("/class/a.txt");
0
 
TimYatesCommented:
where is the file in the jar?

I suspect you'll need:

    object.getClass().getResourceAsStream("/a.txt");
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sudhakar_koundinyaAuthor Commented:
girionis,
 I got that

What is the problem with my code??
0
 
sudhakar_koundinyaAuthor Commented:
>>object.getClass().getResourceAsStream("/a.txt");

Even that doesn't worked for me
0
 
girionisCommented:
Nothing special, it is the same (I think so) as the one TIm suggsted also (putting the / in front of the resource).
0
 
TimYatesCommented:
Test.class.getResourceAsStream( "/a.txt" ) ;

should work too...
0
 
sudhakar_koundinyaAuthor Commented:
>> Test.class.getResourceAsStream( "/a.txt" ) ;

even that doesn't worked

In jar file

class and text file are like this

mytest/Test.class
mytest/a.txt


0
 
TimYatesCommented:
As girionis said then, its

  Test.class.getResourceAsStream( "a.txt" ) ;

(or)

  Test.class.getResourceAsStream( "/mytest/a.txt" ) ;
0
 
sudhakar_koundinyaAuthor Commented:
sorry TimeYates,

After testing all combinations, then only I posted this question

thanks
Sudhakar
0
 
sudhakar_koundinyaAuthor Commented:
Ok, I decieded to use getResource

thanks for helping me :)
0
 
girionisCommented:
:)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now