I have multimodule project
Project
|--src
|-JavaFile.java
Web-Project
|-Web-Content
|-images
| |-logo.PNG
|-pages
|-WEB-INF
- regular java module – contains src with all java files
- dynamic web project module – contains all web related stuff
eventually regular java module goes as a jar file in dynamic web module in lib folder
Problem
java file after compilation looks for an image file in c:\ibm\sdp\server completepath\logo.png rather in context. File is defined in java file as below for iText:
Image logo = Image.getInstance("/images/logo.PNG");
Please suggest how can I change my java file to refer to image. I am not allowed to change my project structure.
You need to use
ServletContext#getResource()or, better,getResourceAsStream()for that. It returns anURLrespectively anInputStreamof the resource in the web content.This way you’re not dependent on where (and how!) the webapp is been deployed. Relying on absolute disk file system paths would only end up in portability headache.
See also:
Update: as per the comments, you seem to be using iText (you should have clarified that a bit more in the question, I edited it). You can then use the
Image#getInstance()method which takes anURL:Update 2: as per the comments, you turn out to be sitting in the JSF context (you should have clarified that as well in the question). You should use
ExternalContext#getResource()instead to get theURL: