Servlet error reading properties file in same dir

Discussion in 'Java' started by Ike, Feb 27, 2006.

  1. Ike

    Ike Guest

    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)
    Ike, Feb 27, 2006
    #1
    1. Advertising

  2. 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(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?)

    --
    John Bollinger
    John C. Bollinger, Feb 28, 2006
    #2
    1. Advertising

  3. Ike

    Ike Guest

    "John C. Bollinger" <> wrote in message
    news:du0aqd$6b3$...
    >
    > 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
    Ike, Feb 28, 2006
    #3
  4. Ike

    Sanjay Guest

    Ike wrote:
    > "John C. Bollinger" <> wrote in message
    > news:du0aqd$6b3$...
    > >
    > > 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
    Sanjay, Feb 28, 2006
    #4
  5. Ike

    trippy Guest

    In article <dzJMf.4398$>, 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(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"
    trippy, Feb 28, 2006
    #5
  6. Sanjay a écrit :
    > Ike wrote:
    >> "John C. Bollinger" <> wrote in message
    >> news:du0aqd$6b3$...
    >>> 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
    Philippe Malka, Feb 28, 2006
    #6
  7. Ike

    Ike Guest

    "Philippe Malka" <> wrote in message
    news:44040d0a$0$5113$...

    > >

    > 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
    Ike, Feb 28, 2006
    #7
  8. Ike

    Ike Guest

    "trippy" <> wrote in message
    news:...

    > // 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
    Ike, Feb 28, 2006
    #8
  9. Ike

    Juha Laiho Guest

    "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.
    --
    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)
    Juha Laiho, Feb 28, 2006
    #9
  10. Ike wrote:
    > "John C. Bollinger" <> wrote in message
    > news:du0aqd$6b3$...
    >
    >>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
    John C. Bollinger, Mar 1, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UnVkeQ==?=

    Sub Dir, Virtual dir, what do I use?

    =?Utf-8?B?UnVkeQ==?=, Jun 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    422
    =?Utf-8?B?UnVkeQ==?=
    Jun 12, 2005
  2. circuit_breaker
    Replies:
    2
    Views:
    1,996
    Jack Jia
    Apr 4, 2004
  3. Matthew Denner
    Replies:
    1
    Views:
    179
  4. Kga Agk
    Replies:
    2
    Views:
    147
    Kga Agk
    Jun 29, 2009
  5. Nick Gnedin
    Replies:
    2
    Views:
    160
Loading...

Share This Page