Servlet error reading properties file in same dir

I

Ike

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(Properties.java:365)
at java.util.Properties.load(Properties.java:293)
at TestServlet3.init(TestServlet3.java:38)
at
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:10
91)
at
org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:750)
at
org.apache.catalina.servlets.InvokerServlet.serveRequest(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.internalDoFilter(Application
FilterChain.java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:173)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:213)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:178)
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.invoke(StandardEngineValve.java
:107)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:307)
at
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:385)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:748)
at
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:678)
at
org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:871)
at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.jav
a:684)
at java.lang.Thread.run(Thread.java:595)
 
J

John C. Bollinger

Ike 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).


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(Properties.java:365)
at java.util.Properties.load(Properties.java:293)
at TestServlet3.init(TestServlet3.java:38)
at
org.apache.catalina.core.StandardWrapper.loadServlet(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?)
 
I

Ike

John C. Bollinger said:
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
 
S

Sanjay

Ike said:
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.
 
T

trippy

Ike 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).


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(propsFile));

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"
 
P

Philippe Malka

Sanjay a écrit :
If it is not able to find the properties file, the problem could be
because of the classpath.
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
 
I

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.


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

Ike

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

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
 
J

Juha Laiho

Ike 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.
 
J

John C. Bollinger

Ike said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top