aspose file tools*
The moose likes Tomcat and the fly likes ClassLoader.getSystemResource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "ClassLoader.getSystemResource" Watch "ClassLoader.getSystemResource" New topic
Author

ClassLoader.getSystemResource

jandries Aldum
Greenhorn

Joined: Jul 18, 2011
Posts: 7
Hi,

I have a webapp and one of the libraries included uses this line to find a property file:


now the folder structure is like a normal webapp, there is a meta-inf, web-inf, res, a few jsps etc. (one of the jsps call a library's method which needs the property file.)

now the question is, where do i have to put the property file in order for the program to be able to read it?
because it just returns null and then throws an exception when the url is supposed to be read (the url is null).

someone said i should put it in the web-inf/lib folder - didnt work
someone said i should put it in tomcat's lib folder - didnt work
someone said i should put it in tomcat's bin folder - didnt work

please help me
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

I'm a little confused here, since your original example seems to be filing a URL under the name of "System.props", instead of something more obvious like "remote.system.url".

One reason your attempts to locate stuff under "web-inf", however is that Java is case sensitive and it should be "WEB-INF".

Th recommended way to pull properties in a J2EE webapp is to put a copy of the properties file in the WEB-INF directory and typically in the WAR classpath (WEB-INF/classes).

To load these properties from a file named "system.properties" into a Java Properties object, you can then invoke getResourceAsStream("/WEB-INF/classes/system.properties"). Note that the property path MUST include the initial "/".


Customer surveys are for companies who didn't pay proper attention to begin with.
jandries Aldum
Greenhorn

Joined: Jul 18, 2011
Posts: 7
Oh yes, sorry, the names are in capitals (WEB-WINF etc).

I thought about using that method (getResourceAsStream() ), but the method that is called(getSystemResourse() ) is inside a library(jar) which is already deployed.
The original idea for the library was to be used in a stand alone application (not in a web app) and it works that way if the System.props file is just in src directory.

but when you make a web app and include the lib, it cant find the system.props file.

If there isn't a way that I can resolve this, ill just have to unpack, change and repack the lib...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

I don't know if I'm understanding that correctly. It sounds like you want to retrieve a property file from inside a JAR that is inside the WAR.

If that's what you want, you have 2 options.

1. Put the jar in the WEB-INF/lib directory. Jars in this directory are included into the webapp's classpath. You could then access the properties via the classloader. Note that ALL files and (especially!) classes in the jar will then appear as part of the classpath for the webapp, so if you just wanted the data, that could be a problem.

2. Put the jar anywhere else and use the getResource() method. You would then have to use the Java utility ZIP classes to retrieve the property "file" from the ZIP (jar) resource.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassLoader.getSystemResource