简体   繁体   English

无法在类路径上加载属性文件; [java,jboss,maven,spring]

[英]Can not load properties file on classpath; [java, jboss, maven, spring]

I have a properties file called app.properties that is correctly put in src/main/resources/app.properties. 我有一个名为app.properties的属性文件,该文件正确放置在src / main / resources / app.properties中。

In the WAR file it is correctly located in \\WEB-INF\\classes. 在WAR文件中,它正确位于\\ WEB-INF \\ classes中。

In Standalone mode in my local environment (windows) and standalone mode on a linux test server, the WAR file starts up correctly reading my properties file. 在我的本地环境(Windows)中的独立模式下以及Linux测试服务器上的独立模式下,WAR文件启动时会正确读取我的属性文件。

In Jboss Domain mode on another linux server, using the exact same WAR file, I get error file not found app.properties. 在另一个Linux服务器上的Jboss域模式下,使用完全相同的WAR文件,我得到未找到app.properties的错误文件。 It is indeed there. 确实在那里。

Other than domain mode being the difference between the two servers, the first test server jboss is installed under root and running as root. 除了域模式是两台服务器之间的区别之外,第一个测试服务器jboss安装在root下并以root身份运行。 The other server is running as a user that has read and execute access. 另一台服务器以具有读取和执行访问权限的用户身份运行。

I've thoroughly debugged the code with print statements and im 99% sure it is not a code issue, any ideas what in jboss domain mode may be causing the problem of not being able to read the properties file on the classpath? 我已经使用print语句彻底调试了代码,并且99%肯定不是代码问题,在jboss域模式下有什么想法可能导致无法读取类路径上的属性文件的问题?

Thanks in advance. 提前致谢。

Relevant parts of code 代码的相关部分

MutablePropertySources sources = new MutablePropertySources();
sources.addLast(getEncryptablePropertiesSource(new ClassPathResource("app.properties")));

partial method 部分方法

private EncryptablePropertiesPropertySource getEncryptablePropertiesSource(Resource propsResource) throws IOException{
    //don't use file system resource because property files may be in a jar
    System.out.println(">>>> in getEncryptablePropertiesSource filename is :");
    System.out.println(propsResource.getFilename());
    System.out.print(">>>> URL is: ");      
    System.out.println(propsResource.getURL());

The last System out print statement throws the error in the 2nd test environment, does not cause problems in any other environment. 最后的System out打印语句在第二个测试环境中引发错误,在任何其他环境中均不会引起问题。

If your ClassPathResource is the class from Spring: 如果您的ClassPathResource是Spring的类:

public class ClassPathResource extends AbstractFileResolvingResource 公共类ClassPathResource扩展AbstractFileResolvingResource

Resource implementation for class path resources. 类路径资源的资源实现。 Uses either a given ClassLoader or a given Class for loading resources. 使用给定的ClassLoader或给定的类加载资源。

Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. 如果类路径资源位于文件系统中,而不是JAR中的资源,则支持将解析为java.io.File。 Always supports resolution as URL. 始终支持将解析作为URL。

Therefore I don't think you can use it in your case. 因此,我认为您无法在自己的情况下使用它。 Have you tried using one of the following methods? 您是否尝试过使用以下方法之一?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM