You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Clark Dorman <cl...@nextcentury.com> on 2005/08/12 15:50:09 UTC

How to find a Properties file

My web service needs to read a properties file but cannot find it.  The
properties file is currently in the jar containing the web service, so
the jar contains things like:

	com.ncc.wrap.Configuration.class
	....
	com.ncc.wrap.StockService.class
	...
	data/config/stock.properties

The Configuration class reads the properties and the StockService class
uses them.  When I run it as a normal jar, Configuration can find the
properties file (the jar is in the classpath), but running under Axis, I
get an exception that it cannot find the properties file.  How do I tell
Configuration to find the file?  The error I get in Tomcat's log is:

com.ncc.wrap.WRAPException: Unable to open file:
data/config/stock.properties
	at com.ncc.wrap.Configuration.initialize(Configuration.java:160)


I have tried to use a servlet to help.  The ConfigurationSerlet.java
class has a function:

  public void init()
    {
        ServletContext sc = getServletConfig().getServletContext();
        try
        {
            Configuration.initialize(sc);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    } 

and I add to the Axis web.xml the following:


  <servlet>
      <servlet-name>WRAPConfiguration</servlet-name>
      <servlet-class>com.ncc.wrap.ConfigurationServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>

and change Configuration.java to include a function that takes a servlet
context in the initialization:

      	String resource = "/" + DEFAULT_CONFIG_FILE;
            InputStream is = sc.getResourceAsStream( resource );
            
            logger.info(" input stream was: " + is + ".  Resource was: "
+ 					resource);
            logger.info("servlet context" + sc );

            wrapProperties.load(is);

However, the problem with this is that the InputStream 'is' is null.
I've tried lots of variations but no luck. 

Any help would be greatly appreciated.

Clark


Re: How to find a Properties file

Posted by Javier Gonzalez <ja...@gmail.com>.
Try putting it in WEB-INF/classes/data/config/stock.properties

On 8/12/05, Clark Dorman <cl...@nextcentury.com> wrote:
> My web service needs to read a properties file but cannot find it.  The
> properties file is currently in the jar containing the web service, so
> the jar contains things like:
> 
>         com.ncc.wrap.Configuration.class
>         ....
>         com.ncc.wrap.StockService.class
>         ...
>         data/config/stock.properties
> 
> The Configuration class reads the properties and the StockService class
> uses them.  When I run it as a normal jar, Configuration can find the
> properties file (the jar is in the classpath), but running under Axis, I
> get an exception that it cannot find the properties file.  How do I tell
> Configuration to find the file?  The error I get in Tomcat's log is:
> 
> com.ncc.wrap.WRAPException: Unable to open file:
> data/config/stock.properties
>         at com.ncc.wrap.Configuration.initialize(Configuration.java:160)
> 
> 
> I have tried to use a servlet to help.  The ConfigurationSerlet.java
> class has a function:
> 
>   public void init()
>     {
>         ServletContext sc = getServletConfig().getServletContext();
>         try
>         {
>             Configuration.initialize(sc);
>         }
>         catch (Exception ex)
>         {
>             ex.printStackTrace();
>         }
>     }
> 
> and I add to the Axis web.xml the following:
> 
> 
>   <servlet>
>       <servlet-name>WRAPConfiguration</servlet-name>
>       <servlet-class>com.ncc.wrap.ConfigurationServlet</servlet-class>
>       <load-on-startup>1</load-on-startup>
>   </servlet>
> 
> and change Configuration.java to include a function that takes a servlet
> context in the initialization:
> 
>         String resource = "/" + DEFAULT_CONFIG_FILE;
>             InputStream is = sc.getResourceAsStream( resource );
> 
>             logger.info(" input stream was: " + is + ".  Resource was: "
> +                                       resource);
>             logger.info("servlet context" + sc );
> 
>             wrapProperties.load(is);
> 
> However, the problem with this is that the InputStream 'is' is null.
> I've tried lots of variations but no luck.
> 
> Any help would be greatly appreciated.
> 
> Clark
> 
> 


-- 
Javier Gonzalez Nicolini

Re: How to find a Properties file

Posted by Jeff Greif <jg...@alumni.princeton.edu>.
If you read the properties file using the stream produced by the method 
ClassLoader.getResourceAsStream
the file will be found in the jar if the jar is in the classpath used by 
the ClassLoader.

Thus,
  Configuration conf = ....
  InputStream stream = conf.getClass().getClassLoader()
      .getResourceAsStream("data/config/stock.properties");
should give the stream you need (or this.getClass().... if used inside 
the constructor of Configuration).

Jeff

Clark Dorman wrote:

>My web service needs to read a properties file but cannot find it.  The
>properties file is currently in the jar containing the web service, so
>the jar contains things like:
>
>	com.ncc.wrap.Configuration.class
>	....
>	com.ncc.wrap.StockService.class
>	...
>	data/config/stock.properties
>
>The Configuration class reads the properties and the StockService class
>uses them.  When I run it as a normal jar, Configuration can find the
>properties file (the jar is in the classpath), but running under Axis, I
>get an exception that it cannot find the properties file.  How do I tell
>Configuration to find the file?  The error I get in Tomcat's log is:
>
>com.ncc.wrap.WRAPException: Unable to open file:
>data/config/stock.properties
>	at com.ncc.wrap.Configuration.initialize(Configuration.java:160)
>
>
>I have tried to use a servlet to help.  The ConfigurationSerlet.java
>class has a function:
>
>  public void init()
>    {
>        ServletContext sc = getServletConfig().getServletContext();
>        try
>        {
>            Configuration.initialize(sc);
>        }
>        catch (Exception ex)
>        {
>            ex.printStackTrace();
>        }
>    } 
>
>and I add to the Axis web.xml the following:
>
>
>  <servlet>
>      <servlet-name>WRAPConfiguration</servlet-name>
>      <servlet-class>com.ncc.wrap.ConfigurationServlet</servlet-class>
>      <load-on-startup>1</load-on-startup>
>  </servlet>
>
>and change Configuration.java to include a function that takes a servlet
>context in the initialization:
>
>      	String resource = "/" + DEFAULT_CONFIG_FILE;
>            InputStream is = sc.getResourceAsStream( resource );
>            
>            logger.info(" input stream was: " + is + ".  Resource was: "
>+ 					resource);
>            logger.info("servlet context" + sc );
>
>            wrapProperties.load(is);
>
>However, the problem with this is that the InputStream 'is' is null.
>I've tried lots of variations but no luck. 
>
>Any help would be greatly appreciated.
>
>Clark
>
>
>  
>