获取java虚拟机地址_java通过虚拟机设置参数的方式动态获取当前项目的绝对路径...

public final static String RUNTIME_HOME = System.getProperty("RUNTIME_HOME");

protected volatile boolean reload = false;

protected final String CONFIG_FILE;

protected final Properties prop = new Properties();

private static CBECConfigFile instance = new CBECConfigFile();

public static CBECConfigFile getInstance(){

return instance;

}

private CBECConfigFile(){

this(RUNTIME_HOME + "/config/" + "cbec-config.properties");

}

protected CBECConfigFile(String configFile){

CONFIG_FILE = configFile;

this.init();

}

/**

* 加载配置文件到内存中

*/

private void init() {

InputStream is = null;

try {

is = new FileInputStream(CONFIG_FILE);

prop.load(is);

} catch (IOException e) {

logger.error("加载配置文件异常:[" + CONFIG_FILE + "], " + e.getMessage(), e);

} finally {

if(is != null){

try {

is.close();

} catch (Exception e) {

}

}

}

}

/**

* 得到配置文件中属性的值

* @param key

* @return

*/

public String getValue(String key) {

if(reload){

synchronized (prop) {

if(reload){

init();

reload = false;

}

}

}

return prop.getProperty(key);

}

public String getValue(String key, String defaultValue) {

if(reload){

synchronized (prop) {

if(reload){

init();

reload = false;

}

}

}

return prop.getProperty(key, defaultValue);

}

/**

* 更改属性的值

* @param key

* @param value

* @param isSave, 是否保存到配置文件中

*/

public synchronized void setValue(String key, String value, boolean isSave) {

prop.setProperty(key, value);

if(isSave){

FileOutputStream os = null;

try {

os = new FileOutputStream(CONFIG_FILE);

prop.store(os, "");

} catch (IOException e) {

logger.error("修改配置文件异常:[" + CONFIG_FILE + "], " + e.getMessage(), e);

} finally {

if(os != null){

try {

os.close();

} catch (Exception e) {

}

}

}

}

}

/**

* 重新加载配置文件中所有属性

*/

public synchronized void reload(){

this.reload = true;;

}

以上代码中,以RUNTIME_HOME的参数方式,动态获取当前类运行的项目的路径,在eclipse的主类上右键配置运行在弹出的窗口中选择自变量tab页的VM自变量中设置

-DRUNTIME_HOME=${workspace_loc:cn.test}参数后,当类运行后就可以取到当前的绝对路径/cn.test目录。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值