• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • 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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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