How to refer to a file present within src/main/resources folder deployed in a jboss env?

拥有回忆 提交于 2020-01-06 04:36:06

问题


I need to look up for an image present in src/main/resources folder in a web application. Its an Apache CXF SAOP based web application.

We are running it in a jboss env(jboss-eap-6.4)

After building the war, the same is deployed.

However, I am unable to get the correct path to the above file. Please suggest.

I have tried multiple options.

File logo= new File("src/main/resources/image.jpg");    
logo.getAbsolutePath(); //  This works great when Junit tested, however breaks with the server. 

Neither does this works-

ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
contextClassLoader.getResource("image.jpg").getPath();

回答1:


Since you're using Maven, files from src/main/resources will end up on the classpath automatically.

To load a resource from the classpath, use something like this:

InputStream in = getClass().getResourceAsStream("/image.jpg");

Getting the path to the resource or opening it as a File may not always work though since the file is probably still stored inside a .war file. class.getResource() will therefore return a URL that is recognizable only by the app server's classloader.



来源:https://stackoverflow.com/questions/54161215/how-to-refer-to-a-file-present-within-src-main-resources-folder-deployed-in-a-jb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!