Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Servlet error reading properties file in same dir

Reply
Thread Tools

Servlet error reading properties file in same dir

 
 
Ike
Guest
Posts: n/a
 
      02-27-2006
I have a servlet which uses public void service() wherein I attempt to load
a properties file, located in the same directory as my servlet.class file
being executed (WEB-INF/classes).


public void init(ServletConfig config) throws ServletException {
// This method initializes the servlet and only gets call once.
// Allocate all of the servlet resources here.
super.init(config);
Properties props = new Properties();
ClassLoader loader = this.getClass().getClassLoader();
InputStream istream = loader.getResourceAsStream("xx.properties");
try{
props.load(istream);//<-----------------this is line 38
driver = props.getProperty("driver");
}catch(.....){...}
}

My xx.properties files merely looks like:

driver = org.gjt.mm.mysql.Driver
url = jdbc:mysql://localhost:3306/dbname?autoReconnect=true

My servlet bombs at line 38 (marked above) with the following stack trace,
below. For the life of me, I cannot figure out why -- if anyone has a clue,
please enlighten me. This used to work perfectly under a different server.
Thanks, Ike:

Feb 27, 2006 10:07:52 AM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Propertie s.java:365)
at java.util.Properties.load(Properties.java:293)
at TestServlet3.init(TestServlet3.java:3
at
org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:10
91)
at
org.apache.catalina.core.StandardWrapper.allocate( StandardWrapper.java:750)
at
org.apache.catalina.servlets.InvokerServlet.serveR equest(InvokerServlet.java
:369)
at
org.apache.catalina.servlets.InvokerServlet.doGet( InvokerServlet.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
at
org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(Application
FilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterCh
ain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.ja
va:213)
at
org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.ja
va:17
at
org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126
)
at
org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105
)
at
org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java
:107)
at
org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:14
at
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyo teHandler.java:307)
at
org.apache.jk.common.HandlerRequest.invoke(Handler Request.java:385)
at org.apache.jk.common.ChannelSocket.invoke(ChannelS ocket.java:74
at
org.apache.jk.common.ChannelSocket.processConnecti on(ChannelSocket.java:67
at
org.apache.jk.common.SocketConnection.runIt(Channe lSocket.java:871)
at
org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.jav
a:684)
at java.lang.Thread.run(Thread.java:595)




 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      02-28-2006
Ike wrote:
> I have a servlet which uses public void service() wherein I attempt to load
> a properties file, located in the same directory as my servlet.class file
> being executed (WEB-INF/classes).
>
>
> public void init(ServletConfig config) throws ServletException {
> // This method initializes the servlet and only gets call once.
> // Allocate all of the servlet resources here.
> super.init(config);
> Properties props = new Properties();
> ClassLoader loader = this.getClass().getClassLoader();
> InputStream istream = loader.getResourceAsStream("xx.properties");
> try{
> props.load(istream);//<-----------------this is line 38
> driver = props.getProperty("driver");
> }catch(.....){...}
> }
>
> My xx.properties files merely looks like:
>
> driver = org.gjt.mm.mysql.Driver
> url = jdbc:mysql://localhost:3306/dbname?autoReconnect=true
>
> My servlet bombs at line 38 (marked above) with the following stack trace,
> below. For the life of me, I cannot figure out why -- if anyone has a clue,
> please enlighten me. This used to work perfectly under a different server.
>
> Feb 27, 2006 10:07:52 AM org.apache.catalina.core.ApplicationContext log
> SEVERE: StandardWrapper.Throwable
> java.lang.NullPointerException
> at java.util.Properties$LineReader.readLine(Propertie s.java:365)
> at java.util.Properties.load(Properties.java:293)
> at TestServlet3.init(TestServlet3.java:3
> at
> org.apache.catalina.core.StandardWrapper.loadServl et(StandardWrapper.java:10
> 91)
> at

[...]

The getResourceAsStream() method (at line 36) is returning null. This
probably means that either the properties file is not present after all,
or that it is present but cannot be opened for reading. (A permissions
problem, perhaps?)

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Ike
Guest
Posts: n/a
 
      02-28-2006

"John C. Bollinger" <(E-Mail Removed)> wrote in message
news:du0aqd$6b3$(E-Mail Removed)...
>
> The getResourceAsStream() method (at line 36) is returning null. This
> probably means that either the properties file is not present after all,
> or that it is present but cannot be opened for reading. (A permissions
> problem, perhaps?)



Thanks John,

That's what I had intially thought. However, it is there, and it does have
read permissions set for owner and group (though, of course, not
public). -Ike


 
Reply With Quote
 
Sanjay
Guest
Posts: n/a
 
      02-28-2006

Ike wrote:
> "John C. Bollinger" <(E-Mail Removed)> wrote in message
> news:du0aqd$6b3$(E-Mail Removed)...
> >
> > The getResourceAsStream() method (at line 36) is returning null. This
> > probably means that either the properties file is not present after all,
> > or that it is present but cannot be opened for reading. (A permissions
> > problem, perhaps?)

>
>
> Thanks John,
>
> That's what I had intially thought. However, it is there, and it does have
> read permissions set for owner and group (though, of course, not


If it is not able to find the properties file, the problem could be
because of the classpath.

> public). -Ike


 
Reply With Quote
 
trippy
Guest
Posts: n/a
 
      02-28-2006
In article <dzJMf.4398$(E-Mail Removed). net>, Ike
took the hamburger, threw it on the grill, and I said "Oh wow"...

> I have a servlet which uses public void service() wherein I attempt to load
> a properties file, located in the same directory as my servlet.class file
> being executed (WEB-INF/classes).
>
>
> public void init(ServletConfig config) throws ServletException {


init() throws ServletException {

ServletConfig config = getServletConfig(); //I think

> // This method initializes the servlet and only gets call once.
> // Allocate all of the servlet resources here.


// no constructor in a servlet, what's the superclass to ServletConfig?


> Properties props = new Properties();


// all you've done at this point is create a blank Properties object
// now you have to use props.load(someInputStream)

// Try this:
// (At top of the class)
// private String propsFile = "/YourFile.properties";
// (In the try block)
// props.load(this.getClass().getResourceAsStream(pro psFile));


>
>
> try{
> props.load(istream);//<-----------------this is line 38
> driver = props.getProperty("driver");
> }catch(.....){...}
> }
>
> My xx.properties files merely looks like:
>
> driver = org.gjt.mm.mysql.Driver
> url = jdbc:mysql://localhost:3306/dbname?autoReconnect=true
>
> My servlet bombs at line 38 (marked above) with the following stack trace,
> below. For the life of me, I cannot figure out why -- if anyone has a clue,
> please enlighten me. This used to work perfectly under a different server.


It did?


--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "He-Man Woman Hater" -- Extreme

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"




 
Reply With Quote
 
Philippe Malka
Guest
Posts: n/a
 
      02-28-2006
Sanjay a écrit :
> Ike wrote:
>> "John C. Bollinger" <(E-Mail Removed)> wrote in message
>> news:du0aqd$6b3$(E-Mail Removed)...
>>> The getResourceAsStream() method (at line 36) is returning null. This
>>> probably means that either the properties file is not present after all,
>>> or that it is present but cannot be opened for reading. (A permissions
>>> problem, perhaps?)

>>
>> Thanks John,
>>
>> That's what I had intially thought. However, it is there, and it does have
>> read permissions set for owner and group (though, of course, not

>
> If it is not able to find the properties file, the problem could be
> because of the classpath.
>
>> public). -Ike

>

Your problem is maybe you have different class loaders e.g. you got your
servlet packaged into a jar file (located in WEB-INF/lib) that will have
its own class loader that cannot load anything from WEB-INF/classes.

Solution is to load your properties from a class that is loaded by the
same class loader.

Philippe


Philippe
 
Reply With Quote
 
Ike
Guest
Posts: n/a
 
      02-28-2006

"Philippe Malka" <(E-Mail Removed)> wrote in message
news:44040d0a$0$5113$(E-Mail Removed)...

> >

> Your problem is maybe you have different class loaders e.g. you got your
> servlet packaged into a jar file (located in WEB-INF/lib) that will have
> its own class loader that cannot load anything from WEB-INF/classes.
>
> Solution is to load your properties from a class that is loaded by the
> same class loader.



Thanks Philippe, but I have them both in WEB-INF/classes -Ike


 
Reply With Quote
 
Ike
Guest
Posts: n/a
 
      02-28-2006

"trippy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> // Try this:
> // (At top of the class)
> // private String propsFile = "/YourFile.properties";
> // (In the try block)
> // props.load(this.getClass().getResourceAsStream(pro psFile));


Thanks trippy,

But I don;t think that differs from what I have, except that it is
composited into one line versus more than one, yes? -Ike


 
Reply With Quote
 
Juha Laiho
Guest
Posts: n/a
 
      02-28-2006
"Ike" <(E-Mail Removed)> said:
>I have a servlet which uses public void service() wherein I attempt to load
>a properties file, located in the same directory as my servlet.class file
>being executed (WEB-INF/classes).
>

....
> ClassLoader loader = this.getClass().getClassLoader();
> InputStream istream = loader.getResourceAsStream("xx.properties");
> try{
> props.load(istream);//<-----------------this is line 38

....
>My servlet bombs at line 38 (marked above) with the following stack trace,
>below. For the life of me, I cannot figure out why -- if anyone has a clue,
>please enlighten me. This used to work perfectly under a different server.


Hmm; looks like you'd rather want to use getResourceAsStream() of either
the Class or ServletContext object of your servlet.

If you use ServletContext.getResourceAsStream(), you'll need to use
full paths, starting from the context root. Looks like what you have
in the code now, and how you expect it to behave, would happen with
Class.getResourceAsStream().

By chance it could work with your current code (so, via
ClassLoader.getResourceAsStream(), but that depends much on the classloader
used, and thus depends much on the server used). Look at the javadocs of
these three similar-yet-different methods, and pay attention to the
differences.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      03-01-2006
Ike wrote:
> "John C. Bollinger" <(E-Mail Removed)> wrote in message
> news:du0aqd$6b3$(E-Mail Removed)...
>
>>The getResourceAsStream() method (at line 36) is returning null. This
>>probably means that either the properties file is not present after all,
>>or that it is present but cannot be opened for reading. (A permissions
>>problem, perhaps?)

>
>
>
> Thanks John,
>
> That's what I had intially thought. However, it is there, and it does have
> read permissions set for owner and group (though, of course, not
> public). -Ike


But is the ownership also correct? That is, is the servlet container
running as either the user that owns the file or a user that is a member
of the group that is assigned to the file? It certainly does seem that
the file is in the classpath.

Are there any other security layers that might be denying access?
SELinux policy, for example?

--
John Bollinger
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dir() vs print(dir()) in the embedded mode Nick Gnedin Python 2 03-30-2013 05:52 AM
External prog get file from main dir, want file in diff dir Kga Agk Ruby 2 06-29-2009 02:09 PM
Behaviour of Dir.glob("**/b") and Dir.glob("*/b") for symlinks Matthew Denner Ruby 1 08-04-2006 03:42 PM
Sub Dir, Virtual dir, what do I use? =?Utf-8?B?UnVkeQ==?= ASP .Net 0 06-12-2005 08:25 PM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM



Advertisments