1

I have a Spring webservice which is deployed as a war in Jboss-eap-6.1.

The code needs to read an image at run time. Iam trying to package the image with the war and deploy it in Jboss.

I make sure that the image is put in /WEB-INF/classes directory of the war. I am trying to read the image this way in the code:

final String path = this.getClass().getClassLoader().getResource("jeffmor.jpg").getPath(); 
File noImage = new File(path);

But the code is not able to pick this image up. If I do a System.out.println in the value of path, it comes as

/content/Service.war/WEB-INF/classes/jeffmor.jpg

Iam not sure whERE the '/content' part got added from? Why is the code not able to pack the image packaged in the war and is there any better way for the code to read a file that is packaged in its own war in Jboss eap 6.1.

user1717230
  • 443
  • 1
  • 15
  • 30

1 Answers1

0

Application server can deploy your archive without unpacking them. You should be using so -

java.lang.ClassLoader.getResourceAsStream("jeffmor.jpg");

Addition.

Your call return a URL of object.

See Also

  • I tried to use this: """"final String path = java.lang.ClassLoader.getResource("jeffmor.jpg").getPath();"""" and i get this error... Cannot make a static reference to the non-static method getResource(String) from the type ClassLoader – user1717230 Jan 29 '14 at 19:53
  • I followed exactly the idea mentioned in the jboss article that you gave and it worked. Had to create a module and put the image in that. Thanks. – user1717230 Jan 29 '14 at 20:19
  • Just realised that the image was put in a module in Jboss, but not packaged in the war. Any other way of adding the image from the war file to the applications class path ? – user1717230 Jan 29 '14 at 20:24
  • That only works after I add the image to the classpath by putting in the module as mentioned in the article. Without adding it to the module in Jboss, the code you mentioned is not picking the image up. – user1717230 Jan 29 '14 at 20:32