0

I've created dynamic web project in Eclipse and added it to ear project.

Image resources are placed at src\resources\

Code use resources:

@GET
@Path("bank")
@Produces("application/json")
public String getBank(@Context HttpHeaders headers) {
  ClassLoader classLoader = getClass().getClassLoader();
  File file = new File(classLoader.getResource("/resources/alfa.png").getFile());
  String base64 = Utility.getBase64(file);
  return "{\"icon\":\"" + base64 + "\"}";
}

While debugging it's OK.

When ear deployed code throws exception: java.io.FileNotFoundException: C:\apps\wildfly-8.2.0.Final\bin\content\TimerService.ear\timer.war\WEB-INF\classes\resources\alfa.png

All resources are present in war-file contained in ear-file.

What is wrong?

Belowee P.
  • 35
  • 11
  • You can't reference class path resources as java.o.File objects because they will often not exist file system as distinct file system objects. Modify Utility.getBase64 to take a java.io.InputStream instead. – Steve C Jan 10 '16 at 10:32
  • Check out all the answers at [getResourceAsStream() vs FileInputStream](http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream) for more information. I'm sure your question is a dupe but I can't find a good match – Steve C Jan 10 '16 at 10:41
  • Thanks Steve. getResourceAsStream() is solution. – Belowee P. Jan 11 '16 at 07:50

1 Answers1

0

this way it should work

@GET
@Path("bank")
@Produces("application/json")
public String getBank(@Context final HttpServletRequest request) {
  ClassLoader classLoader = request.getServletContext().getClassLoader();
  File file = new File(classLoader.getResource("/resources/alfa.png").getFile());
  String base64 = Utility.getBase64(file);
  return "{\"icon\":\"" + base64 + "\"}";
}
guleryuz
  • 2,481
  • 1
  • 13
  • 17