0

I am using JBoss4.0.1 and Struts2.1.6

I have an application which have some configuration file(appConfig.xml in folder WEB-INF/config). This configuration file contains the relative paths of other files to be read.(other.xml, some.xml etc). Application is deployed as .war inside default/deploy

I have a Utility package that reads the configuration files. I have deployed the utility.jar inside default/lib

In contextInitialized method of ServletContextListener I am reading the appConfig.xml and passing IOStream on appConfig to my Utility Class, which reads this file and loads all the context relative paths.

How can I read these files now? Because if I try to create an input stream it is returned NULL.

I have tried following inside Utility Class.

read method is called from inside the contextInitialized method of ServletContextListener

public void read(){ 
this.getClass().getClassLoader.getResourceAsStream("/WEB-INF/config/some.xml");
}

public void read(ClassLoader cl){ 
  cl.getResourceAsStream("/WEB-INF/config/some.xml"); --> Null
  cl.getResourceAsStream("/../config/some.xml"); --> Null
  cl.getResourceAsStream("../config/some.xml"); --> Null
}

public void read(ServletContext ct){ 
 ct.getResourceAsStream("/WEB-INF/config/some.xml");.
 ct.getResourceAsStream("/../config/some.xml"); --> Null
 ct.getResourceAsStream("../config/some.xml"); --> Null
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Muhammad Shahid
  • 69
  • 3
  • 11
  • I have tried to similar question in [this post](http://stackoverflow.com/questions/4585553/how-to-load-resource-from-jar-file-packaged-in-a-war-file/16082211#16082211). Hope this helps. – Rupesh Apr 18 '13 at 11:56

1 Answers1

0

None of following would work because /WEB-INF/config is not in classpath so classloader doesn't know anything about it,

public void read(){ 
this.getClass().getClassLoader.getResourceAsStream("/WEB-INF/config/some.xml");
}

public void read(ClassLoader cl){ 
  cl.getResourceAsStream("/WEB-INF/config/some.xml"); --> Null
  cl.getResourceAsStream("/../config/some.xml"); --> Null
  cl.getResourceAsStream("../config/some.xml"); --> Null
}

Those 2 doesn't work because it goes out of the context (.. can't be first element),

public void read(ServletContext ct){ 

 ct.getResourceAsStream("/../config/some.xml"); --> Null
 ct.getResourceAsStream("../config/some.xml"); --> Null
}

I don't quite understand your intention. If you know the file is in "/WEB-INF/config/some.xml", why do you want read it from "/../config/some.xml"?

It's not a good idea to store configuration file inside war. You should consider moving it somewhere else.

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • thanks for your comments, Just want to add a couple fo things for your understanding. 1. WEB-INF/config is in added in class path 2. this is one time configuration so i need it inside the .war. 3. there can be multiuple application context to have the same configuration so i created one utility package which reads this kind of configuration. Now from within the context i want the utility to read my configuration file. i hope this will better help you understand the problem your help is appreciated. – Muhammad Shahid Sep 07 '09 at 10:56