0

I have multimodule project

Project
  |--src
      |-JavaFile.java

Web-Project
  |-Web-Content
    |-images
    |   |-logo.PNG
    |-pages
    |-WEB-INF
  1. regular java module - contains src with all java files
  2. 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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Pradeep Kumar
  • 79
  • 1
  • 10

1 Answers1

1

You need to use ServletContext#getResource() or, better, getResourceAsStream() for that. It returns an URL respectively an InputStream of the resource in the web content.

InputStream input = getServletContext().getResourceAsStream("/images/logo.PNG");
// ...

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 an URL:

URL url = getServletContext().getResource("/images/logo.PNG");
Image image = Image.getInstance(url);
// ...

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 the URL:

URL url = FacesContext.getCurrentInstance().getExternalContext().getResource("/images/logo.PNG");
Image image = Image.getInstance(url);
// ...
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Question: I need absolute URL or Absolute filename -> String rather stream. I am using com.itextpdf.text.Image; Thank you very much for you kind reply. I am using faces therefore I had to use InputStream input = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/images/logo.PNG"); Image logo = Image.getInstance(input); – Pradeep Kumar Oct 21 '11 at 17:20
  • I do not have direct access to Servlet Context I go through FacesContext. Now I get NullPointerException. InputStream input = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("../images/logo.PNG"); String str=input.toString(); byte[] b=str.getBytes(); Image logo = Image.getInstance(b); – Pradeep Kumar Oct 21 '11 at 18:23
  • My bad wrong file Name I had... Now its throwing java.io.IOException: The byte array is not a recognized imageformat. – Pradeep Kumar Oct 21 '11 at 18:33
  • You're using JSF? You should have mentioned that as well :) I'll update the answer. Your mistake is by the way caused because you did a nonsensicial `toString()` on the `InputStream` instead of reading it using `read()`. – BalusC Oct 21 '11 at 18:37
  • After a search on how to change from inputStream to byte array I was able to resolve the problem. Thanx alot BalusC you are always a great help. – Pradeep Kumar Oct 21 '11 at 18:44