File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Problem with static block and getResourceAsStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem with static block and getResourceAsStream" Watch "Problem with static block and getResourceAsStream" New topic
Author

Problem with static block and getResourceAsStream

Alex Florentino
Ranch Hand

Joined: Dec 12, 2006
Posts: 48
I have the code:


I was sure that a file "file.properties" exists, but I got this error:


suggestions ?

thanks.

[ September 28, 2007: Message edited by: Alex Florentino ]

[ September 28, 2007: Message edited by: Alex Florentino ]
[ September 28, 2007: Message edited by: Alex Florentino ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

I'd think that if the file could not be found, it would throw a FileNotFound exception. You could test if in is null. If it is, the file is not found.
Your exception seems to come from within the Properties class which leads me to believe you have a malformed properties file. What does file.properties look like?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Originally posted by Joe Ess:
I'd think that if the file could not be found, it would throw a FileNotFound exception. You could test if in is null. If it is, the file is not found.
Your exception seems to come from within the Properties class which leads me to believe you have a malformed properties file. What does file.properties look like?

getResource and getResourceAsStream both return null if the resource cannot be found, both in Class and in ClassLoader. The properties file itself is probably correct, it just can't be found.

Alex, you do know that is identical to ? In other words, you are trying to find file.properties in the Java core libraries, in package java.lang.
You should probably try MyClass.class.getResourceAsStream instead, so the file.properties file will be searched locally to the current class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Rob Prime:

you are trying to find file.properties in the Java core libraries, in package java.lang.

Yep, if you do not want the properties file to be searched in the package structure of the class, you can use <Your class name>.class.getClassLoader().getResourceAsStream(). This method will find the properties in the classpath and not in a specific package structure.


apigee, a better way to API!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Actually, omitting the "getClassLoader()" part allows you to use relative resource paths.

ClassLoader.getResource(AsStream) requires a full resource path, starting at /. Class.getResource(AsString) will make it absolute using the current class location. For instance, using class mypackage.test.MyTest, using resource "mytest.properties" will resolve to "/mypackage/test/mytest.properties". This is then passed to the class' ClassLoader anyway.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Actually, omitting the "getClassLoader()" part allows you to use relative resource paths.

Absolutely. The choice of the method to use depends on where your resource is placed.

ClassLoader.getResource(AsStream) requires a full resource path, starting at /.

The resource file must be in the classpath and all the entires in the classpath are absolute.

Class.getResource(AsString) will make it absolute using the current class location. For instance, using class mypackage.test.MyTest, using resource "mytest.properties" will resolve to "/mypackage/test/mytest.properties".

mmmmmm ... this will become mypackage/test/mytest.properties and not /mypackage/test/mytest.properties i.e. it will still not be an absolute path but relative to any of the entries in the classpath.

[ October 01, 2007: Message edited by: Nitesh Kant Trying to put emphasis on "/" ]

[ October 01, 2007: Message edited by: Nitesh Kant hmmmm .... ]
[ October 01, 2007: Message edited by: Nitesh Kant ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with static block and getResourceAsStream