20

I have a plain java class in a web application and want to read a configuration file under WEB-INF folder. I know the way to access the file if its in the classpath (WEB-INF/classes folder). Since WEB-INF/classes folder is meant for .class files, I want to keep my configuration file under WEB-INF folder only.

Can anyone tell me how I can access it from my java class?

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
Newbie
  • 2,979
  • 9
  • 34
  • 41
  • 1
    If the configuration file is for the Java classes, why put it in the `WEB-INF` folder... why not in the `src/main/resources` folder and access it from there? – Mr T. Oct 14 '12 at 15:54
  • I would say that `getResourceAsStream` should work – SJuan76 Oct 14 '12 at 15:54
  • 1
    The configuration should be in src/main/resources, and will be built into class folder when deploy the web app. Why do you need to put the configuration file into WEB-INF folder? – Thinhbk Oct 14 '12 at 15:56
  • I (tentatively) agree with @Thinhbk: the WEB-INF is a location for configuration for web-related components, like servlets and filters. Configuration for 'plain Java class' components should be on the classpath. Maybe. – Tom Anderson Oct 14 '12 at 16:28
  • I feel `WEB-INF/classes` folder should not contain any configuration files. – Newbie Oct 14 '12 at 16:52

4 Answers4

22

ServletContext.getResourceAsStream() will load a file from a given path relative to the root of the WAR file. Something like:

ServletContext ctx;
InputStream configStream = ctx.getResourceAsStream("/WEB-INF/config.properties");

The major issue here is that you need access to the servlet context to be able to do this. You have that in a servlet or a filter, but not in a non-web component further back in the application. You have a few options:

  • Make the servlet context available from the web layer to the service layer, via an application-scoped variable, or injection, or some other way
  • Put the resource-loading code in the web layer, and expose that to the service layer
  • Load the configuration in the web layer, and pass it on to the service layer
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
  • 1
    No direct way of doing this in non-web component like plain java class? – Newbie Oct 14 '12 at 16:54
  • 2
    Not that i know of. A plain Java class has no idea it's part of a web application, so there's no way it could have access to any API for doing things specific to web applications! – Tom Anderson Oct 14 '12 at 20:09
2

You can get the absolute path of servlet using getRealPath() method of ServletContext and then append WEB-INF to the path you get. I think this is very basic there may be some other answers as well.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
  • 2
    I know how to do this with the help of `ServletContext`. But what I am looking for is to do this in a plain java class. – Newbie Oct 14 '12 at 16:56
1

hey you all care for context related file loading like application context , web.xml ,config and property file

Here is how to load a java file any kind of file under WEB-INF but it stored on another stucture like a sub folder reportFile the your file or sub folder again report01--

fullpath is = /WEB-INF/reportFile/report01/report.xml ,i have tried many possibilities to load and read this xml file ...none of the above worked for me but , here is the trick for future use...

In Action or inservice class you know interface implementation class no imports that is good part also.

declare your file object

File myClass = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getFile());
System.out.println("Finding calss path first then remove classes from the path "    + myClass.getCanonicalPath().replaceFirst("classes", "")+"reportFIle/report01/reports.xml")

2.Load the path by removing classes from the above and add your specific path

File f = new File(myClass.getCanonicalPath().replaceFirst("classes", "")+"reportFile/report01/reports.xml")

Then

you can even parse it using xml parser or do anything

document = docBuilder.parse(new File(myClass.getCanonicalPath().replaceFirst("classes", "")+"reportFile/report01/reports.xml"));

Cheers!!

Develop4Life
  • 7,581
  • 8
  • 58
  • 76
1

"new FIleInputStream( Utility.class.getClassLoader().getResource(keyFileName).getPath() )" worked for me.

Here "Utility" is my class name where the code is calling this line , "keyFileName" is the file i need to open

Sahan Maldeniya
  • 1,028
  • 1
  • 14
  • 21