Solved

getResourceAsStream is returning null

Posted on 2004-09-01
12
537 Views
Last Modified: 2010-03-31
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
Comment
Question by:sudhakar_koundinya
  • 6
  • 3
  • 3
12 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
Comment Utility
What happens if you do this:

URL url = getClass().getResource("a.txt");
InputStream is = url.openStream();
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
even I have tried this also

object.getClass().getResourceAsStream("/class/a.txt");
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
where is the file in the jar?

I suspect you'll need:

    object.getClass().getResourceAsStream("/a.txt");
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
girionis,
 I got that

What is the problem with my code??
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
>>object.getClass().getResourceAsStream("/a.txt");

Even that doesn't worked for me
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Nothing special, it is the same (I think so) as the one TIm suggsted also (putting the / in front of the resource).
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Test.class.getResourceAsStream( "/a.txt" ) ;

should work too...
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
>> 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
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
As girionis said then, its

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

(or)

  Test.class.getResourceAsStream( "/mytest/a.txt" ) ;
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
sorry TimeYates,

After testing all combinations, then only I posted this question

thanks
Sudhakar
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
Comment Utility
Ok, I decieded to use getResource

thanks for helping me :)
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
:)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now