java 使用 Jboss7 加载资源返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10833046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load Resource returns null with Jboss7
提问by Pierre
How do I load resources like images from the java code with Jboss7.1?
如何使用 Jboss7.1 从 java 代码加载图像等资源?
This used to work with Jboss4:
这曾经与 Jboss4 一起使用:
this.getClass().getClassLoader().getResourceAsStream("/myapp/includes/images/image1.png");
Now this returns null.
现在这将返回 null。
What is best practice for loading resources in java code now with Jboss7?
现在使用 Jboss7 在 Java 代码中加载资源的最佳实践是什么?
I did some testing:
我做了一些测试:
URL url = this.getClass().getResource("");
System.out.println(url);
url = this.getClass().getResource("../../../");
System.out.println(url);
url = this.getClass().getResource("../../../../");
System.out.println(url);
url = this.getClass().getResource("../../../../../");
System.out.println(url);
url = this.getClass().getResource("includes");
System.out.println(url);
13:33:49,143 INFO [stdout] (http--127.0.0.1-8080-1) vfs:/C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone/deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/xxx/yyy/beans/jsf/
13:33:49,144 INFO [stdout] (http--127.0.0.1-8080-1) vfs:/C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone/deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/xxx/
13:33:49,150 INFO [stdout] (http--127.0.0.1-8080-1) jar:file:/C:/Eclipse/apps/jboss-as-7.1.1.Final/modules/javax/activation/api/main/activation-1.1.1.jar!/com/
13:33:49,151 INFO [stdout] (http--127.0.0.1-8080-1) file:/C:/Eclipse/apps/jboss-as-7.1.1.Final/modules/sun/jdk/main/service-loader-resources/
13:33:49,152 INFO [stdout] (http--127.0.0.1-8080-1) null
采纳答案by JoseK
As documentedon the JBoss community wiki (without using a jboss-deployment-structure.xml
file):
作为记录JBoss的社区维基(不使用jboss-deployment-structure.xml
文件):
- Create a module for the configuration file (
jboss-as-7/modules/com/yourcompany/configuration/main/module.xml
):
- 为配置文件 (
jboss-as-7/modules/com/yourcompany/configuration/main/module.xml
)创建一个模块:
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mycompany.configuration">
<resources>
<resource-root path="."/>
</resources>
</module>
- Add properties to the module:
- 向模块添加属性:
jboss-as-7/
modules/
com/
yourcompany/
configuration/
main/
module.xml
settings.properties
other-settings.xm
- Add the module to the
CLASSPATH
using aMANIFEST.MF
entry:
- 将模块添加到
CLASSPATH
usingMANIFEST.MF
条目:
Manifest-Version: 1.0
Dependencies: com.mycompany.configuration
- Load a properties file from the CLASSPATH:
- 从 CLASSPATH 加载属性文件:
InputStream settingsStream =
getClass().getClassLoader().getResourceAsStream("settings.properties");
I had to create a module folder called com/mycompany/main
and add all the images in there. So this sits outside the WAR at least though within JBOSS_HOME
. Then I could load the image using:
我必须创建一个名为的模块文件夹com/mycompany/main
并在其中添加所有图像。所以这至少在 WAR 之外,尽管在JBOSS_HOME
. 然后我可以使用以下方法加载图像:
URL imgUrl = this.getClass().getClassLoader().getResource("myimage.jpg");
回答by Thor
The method this.getClass().getClassLoader().getResourceAsStream("path")
is handling the pathfrom the root of your classpath. In a WAR this is normally WEB-INF/classes
and WEB-INF/lib
.
该方法this.getClass().getClassLoader().getResourceAsStream("path")
正在处理从类路径的根开始的路径。在 WAR 中,这通常是WEB-INF/classes
和WEB-INF/lib
。
Your path WebContent/includes/images/
seems to be inside the root of your WAR file (which is not the classpath). So you can
您的路径WebContent/includes/images/
似乎在 WAR 文件的根目录中(不是类路径)。所以你可以
- use
getResourceAsStream
ofServletContext
(see here) which handles paths to the root of the WAR file. - package all of your images in a additional JAR (e.g.
my-app.resources.jar
) and useClassLoader.getResourceAsStream()
- move your images in the classpath (see above).
- 使用
getResourceAsStream
的ServletContext
(见此处),该把手路径WAR文件的根目录。 - 将所有图像打包在一个额外的 JAR(例如
my-app.resources.jar
)中并使用ClassLoader.getResourceAsStream()
- 在类路径中移动您的图像(见上文)。
回答by Prakhyat
The class loading in JBOSS 7 is based upon JBOSS module project. All earlier versions loading is in a hierarchical manner.
JBOSS 7 中的类加载基于 JBOSS 模块项目。所有早期版本的加载都是以分层方式进行的。
In earlier versions of JBOSS we can put/specify the external folder containing all the app properties in the classpath attribute by modifying run.sh/run.bat.
在早期版本的 JBOSS 中,我们可以通过修改 run.sh/run.bat 在 classpath 属性中放置/指定包含所有应用程序属性的外部文件夹。
But in JBOSS 7 since the loading is based on JBOSS module project, the external properties configuration is different.
但是在 JBOSS 7 中,由于加载是基于 JBOSS 模块项目,外部属性配置是不同的。
You can try configuring/loading the properties file in JBOSS 7 in below two ways,
您可以尝试通过以下两种方式在 JBOSS 7 中配置/加载属性文件,
Prepare jar having all the property files required for your app. Use this jar in your war/ear.
Then any property file from this jar can be loaded using below code,
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) ;
The changes required in JBOSS 7 to specify external property files is available in community link https://community.jboss.org/wiki/HowToPutAnExternalFileInTheClasspath. If the changes are done according to this link, deployment will work. Make sure after configuration in JBOSS 7, the below code has to be used to load properties,
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("xyz.properties");
准备具有应用程序所需的所有属性文件的 jar。在你的War/耳朵中使用这个罐子。
然后可以使用以下代码加载此 jar 中的任何属性文件,
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
JBOSS 7 中指定外部属性文件所需的更改可在社区链接https://community.jboss.org/wiki/HowToPutAnExternalFileInTheClasspath 中找到。如果根据此链接完成更改,则部署将起作用。确保在 JBOSS 7 中配置后,必须使用以下代码来加载属性,
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("xyz.properties");