简体   繁体   中英

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.

In the WAR file it is correctly located in \\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.

In Jboss Domain mode on another linux server, using the exact same WAR file, I get error file not found 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. 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?

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.

If your ClassPathResource is the class from Spring:

public class ClassPathResource extends AbstractFileResolvingResource

Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.

Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.

Therefore I don't think you can use it in your case. Have you tried using one of the following methods?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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