My web application is based on Google Maps API and I have problems trying to access an image which is within a jar file.
I want to be able to do two things:
- Show a marker in the javascript map.
- Show a marker in a static map.
In both cases I need to provide the path to the image file. This image is inside a jar file which is located in the /WEB-INF/lib folder once the web application is deployed in the server.
I thought that the following line would work:
iconMarker.image = “WEB-INF/lib/cimCore.jar!/META-INF/resources/img/” + ilMarker.icon;
But it shows the following error in the client browser:
“NetworkError: 404 No Found- http://ip/cimWeb/WEB-INF/lib/cimCore.jar!/META-INF/resources/img/gsearch.png“
The path is OK so I guess that accessing by means of ‘!’ character is not the proper way.
Any idea?
You’ll have to use a servlet which will load the image bytes from the classloader, and stream these bytes to the response :
MyServlet.class.getResourceAsStream("/META-INF/resources/img/gsearch.png")to get an input stream on the imageresponse.setContentType("image/png")Note that most of the MVC frameworks have support for mapping a pattern of URLs to classpath resources, or to at least implement one part of the algorithm above for you.
Or you can just extract these images out of the jar file, and put them in the webapp’s tree of externally accessible resources.
Side note: WEB-INF is, by design, not accessible from the outside. If what you wanted to do was possible, anybody could steal the jar files and configuration of any Java-based webapp.