日期:2014-05-17  浏览次数:20909 次

请求帮忙解决下路径问题
昨天写了个java项目,测试mybatis的,
这是我的目录结构,我的DBUtil里是按照文档上写的
 private DBUtil() {     
        try {
            String resource = "./config/Configuration.xml";
            Reader reader = Resources.getResourceAsReader(resource); 
            log.info(reader);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            reader.close();                   
        } catch (IOException e) { 
         log.error(e.toString());
            e.printStackTrace();     
        }     
    }     
这时候我测试的时候,能够找到Configuration.xml,当我把这个项目打成jar包添加到别的项目的时候,写了个测试,但这个时候却说找不到./config/Configuration.xml,请各位大大帮帮小弟,这个路径该怎么写,打成JAR包,调用JAR时候才能找到。

------解决方案--------------------
之前我也遇到过这样的问题。主要是你那resource,打成jar找不到了
你可以这样写
public String getClassesRootPath(){
URL url = getClass().getClassLoader().getResource("");
String strURL = url.toString();
if(strURL.indexOf("file:")>=0){
strURL = strURL.substring(6);
}
String[] tempstr = strURL.split("/");
strURL = "";
for(int i=0; i<tempstr.length; i++){
strURL += tempstr[i] + "/";
if(tempstr[i].toLowerCase().equals("classes")){
break;
}
}
return strURL;
}
 你改改我给你方法就行了,因为我的文件是放到src下的,你将这个方法返回的值放到你的
Quote:
getResourceAsReader(resource)
Quote:
里面就行了。