I need to access the resource files in my web project from a class. The problem is that the paths of my development environment are different from the ones when the project is deployed.
For example, if I want to access some css files while developing I can do like this:
File file = new File("src/main/webapp/resources/styles/some.css/");
But this may not work once it’s deployed because there’s no src or main directories in the target folder. How could I access the files consistently?
You seem to be storing your CSS file in the classpath for some unobvious reason. The folder name
srcis typical as default name of Eclipse’s project source folder. And that it apparently magically works as being a relative path in theFileconstructor (bad, bad), only confirms that you’re running this in the IDE context.This is indeed not portable.
You should not be using
File‘s constructor. If the resource is in the classpath, you need to get it as resource from the classpath.Assuming that the current class is running in the same context, this will work regardless of the runtime environment.
See also:
Update: ah, the functional requirement is now more clear.
Use
getResource()instead to obtain it as anURL. Then you can open the connection on it and request for thelastModified.