野飞:从类路径目录读取属性文件

huangapple go评论68阅读模式
英文:

wildfly: reading properties file from classpath directory

问题

我正试图从我的类路径文件中读取部署特定信息。通过域模式在Wildfly 13上部署War文件。

/opt/wildfly/domain/servers/APPS1/tmp/vfs/temp/tempe22b1a59c629344/content-35e1234535af2e86/WEB-INF/classes/bis/com/test/config/application.properties

当我尝试使用以下方式获取路径时:

System.getProperty(jboss.server.temp.dir) 

我只得到了/opt/wildfly/domain/servers/APPS1/tmp

我从这个类中运行这段代码 bis.com.test.module.app

bis|com|test|module|app.class

bis|com|test|config|application.properties

我尝试过

private static final String PATH = "/WEB-INF/classes/bis/com/test/config/application.properties";


Properties props = new Properties();
InputStream in = getClass().getResourceAsStream("/application.properties");
props.load(in);
String JDBC = props.getProperty("jdbc.connection");


Properties props = new Properties();
InputStream in = App.class().getResourceAsStream("/application.properties");
props.load(in);
String JDBC = props.getProperty("jdbc.connection");


Properties props = new Properties();
InputStream in = getClass().getResourceAsStream(PATH);
props.load(in);
String JDBC = props.getProperty("jdbc.connection");


URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(PATH);
Properties app = new Properties();
           app.load(url.openStream());
String JDBC = app.getProperty("jdbc.connection");

然而我得到了所有情况下的空指针异常...我如何绕过/访问/检索临时文件夹并进入WEB-INF文件夹?由于当前的设计逻辑,我无法将应用程序属性移动到其他位置。我只能在这里访问属性文件。怎么办?

英文:

I'm trying to read deployment specific information from a properties file in my claspath folder. War file is deployed on wildfly 13 via domain mode.

/opt/wildfly/domain/servers/APPS1/tmp/vfs/temp/tempe22b1a59c629344/content-35e1234535af2e86/WEB-INF/classes/bis/com/test/config/application.properties

When i try to get my path using the following

System.getProperty(jboss.server.temp.dir) 

I only get /opt/wildfly/domain/servers/APPS1/tmp

I am running this code from this class bis.com.test.module.app

bis|com|test|module|app.class

bis|com|test|config|application.properties

I have tried

private static final String PATH = "/WEB-INF/classes/bis/com/test/config/application.properties";


Properties props = new Properties();
InputStream in = getClass().getResourceAsStream("/application.properties");
props.load(in);
String JDBC = props.getProperty("jdbc.connection");


Properties props = new Properties();
InputStream in = App.class().getResourceAsStream("/application.properties");
props.load(in);
String JDBC = props.getProperty("jdbc.connection");


Properties props = new Properties();
InputStream in = getClass().getResourceAsStream(PATH);
props.load(in);
String JDBC = props.getProperty("jdbc.connection");


URL url = FacesContext.getCurrentInstance().getExternalContext().getResource(PATH);
Properties app = new Properties();
           app.load(url.openStream());
String JDBC = app.getProperty("jdbc.connection");

However im getting null pointer exception for all...how do i bypass/access/retrieve the temp folders and go into the WEB-INF folders? Im unable to move the application properties elsewhere due to current design logic. I only can access the properties file here. how?

答案1

得分: 1

在一番搜索后,我发现您的 application.properties 位于一个包下,因此应该按照以下所示进行访问:

ClassLoader loader = getClass().getClassLoader();
inputStream = loader.getResourceAsStream("/bis/com/test/config/application.properties");
英文:

After some digging, I found that your application.properties reside under a package so it should be accessed as given below:

ClassLoader loader = getClass().getClassLoader();
inputStream = loader.getResourceAsStream("/bis/com/test/config/application.properties");

huangapple
  • 本文由 发表于 2020年9月19日 13:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63965626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定