我有简单的java项目结构:
I have simple java project with structure:
package com.abc: a.java b。 java c.properties
我在c.properties文件中配置了数据库配置参数。 在a.java和b.java中,我使用以下方法加载属性文件:
I have database configuration parameters configured in c.properties file. Inside a.java and b.java, I am loading properties file using:
Properties p = new Properties(); InputStream in = this.getClass().getResourceAsStream("c.properties"); p.load(in);这很好用。但主要问题是,一旦我通过导出此代码准备可执行jar,属性文件也会打包在jar文件中。如果其他人想要修改不同数据库配置的属性文件,他怎么能这样做? 我是否必须将属性文件存储在本地计算机的某个固定位置。例如C:/。然后将jar和属性文件一起提供给另一个人。然后他需要复制C:/ location中的属性文件? 还有一个问题,我怎样才能使这个位置通用于Windows和Linux机器?
This works fine. But the main question is, once I prepare executable jar by exporting this code, properties file also gets packaged in jar file. If someone else wants to modify properties file for different database configuration, how can he do it? Do I have to store properties file in some fixed location in local machine. e.g. "c:/". Then give jar along with properties file to the other person. Then he needs to copy properties file inside C:/ location? Also one more question, how can i make this location generic for windows and linux machine?
推荐答案典型方式处理此问题的方法是从嵌入式文件加载基本属性,并允许应用程序的用户指定包含替代的其他文件。一些伪代码:
The typical way of handling this is to load the base properties from your embedded file, and allow users of the application to specify an additional file with overrides. Some pseudocode:
Properties p = new Properties(); InputStream in = this.getClass().getResourceAsStream("c.properties"); p.load(in); String externalFileName = System.getProperty("app.properties"); InputStream fin = new FileInputStream(new File(externalFileName)); p.load(fin);您的程序将被调用类似于:
Your program would be invoked similar to this:
java -jar app.jar -Dapp.properties="/path/to/custom/app.properties"更多推荐
对java属性文件位置感到困惑
发布评论