Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getResourceAsStream is returning null

Posted on 2004-09-01
12
Medium Priority
?
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 35

Accepted Solution

by:
girionis earned 200 total points
ID: 11956672
What happens if you do this:

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

Author Comment

by:sudhakar_koundinya
ID: 11956680
even I have tried this also

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

Expert Comment

by:TimYates
ID: 11956779
where is the file in the jar?

I suspect you'll need:

    object.getClass().getResourceAsStream("/a.txt");
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11956783
girionis,
 I got that

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

Author Comment

by:sudhakar_koundinya
ID: 11956788
>>object.getClass().getResourceAsStream("/a.txt");

Even that doesn't worked for me
0
 
LVL 35

Expert Comment

by:girionis
ID: 11956795
Nothing special, it is the same (I think so) as the one TIm suggsted also (putting the / in front of the resource).
0
 
LVL 35

Expert Comment

by:TimYates
ID: 11956800
Test.class.getResourceAsStream( "/a.txt" ) ;

should work too...
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11956849
>> 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
ID: 11956980
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
ID: 11957002
sorry TimeYates,

After testing all combinations, then only I posted this question

thanks
Sudhakar
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11957035
Ok, I decieded to use getResource

thanks for helping me :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11957616
:)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

596 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