• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with static block and getResourceAsStream

 
Alex Florentino
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9256
9
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic