user3640525
user3640525

Reputation: 1

Problems with relative paths in Eclipse.

I am trying to use relative paths in a Dynamic Web Project in Eclipse. I am NOT using them in the Servlet, but in another class that is called by the Servlet. The file I am trying to access to is a property file located in

MyProject/WebContent/WEB-INF/propertyFile.properties

I have tried almost every relative path...

WebContent/WEB-INF/propertyFile.properties 
/WebContent/WEB-INF/propertyFile.properties
./WebContent/WEB-INF/propertyFile.properties
MyProject/WebContent/WEB-INF/propertyFile.properties
/MyProject/WebContent/WEB-INF/propertyFile.properties
WEB-INF/propertyFile.properties
/WEB-INF/propertyFile.properties
./WEB-INF/propertyFile.properties

...and so on...what can I do?

Thanks in advance!

Upvotes: 0

Views: 354

Answers (1)

JB Nizet
JB Nizet

Reputation: 691635

You nee to understand that file IO read files, from the filesystem, and relative to the directory from which the JVM (i.e. your web container) is started.

Remember that, once your app is deployed to production, there won't be any MyProject or WebContent folder. That's what exists on your development machine. The only thing that will exist in production is the war file deployed in the application server.

What you actually want is to load a resource, located in the deployed web application (i.e. which is inside your war file).

To do that, you must use ServletContext.getResourceAsStream():

InputStream in = servletContext.getResourceAsStream("/WEB-INF/propertyFile.properties");

Upvotes: 2

Related Questions