把一个应用打成jar包,jar包的目录为com.test,在com.test下有个配置文件config.properties。同时,在com.test.util下有个类ConfigUtil.java,要在这个类的一个静态块中加载这个配置文件,怎样可以找到它的路径。我用ConfigUtil.class.getResource("config.properties")取出来的是null。

解决方案 »

  1.   

    String path = (this.getClass()).getProtectionDomain().getCodeSource()
    .getLocation().getFile();//可以得到类的物理路径,其他的你自己想吧
      

  2.   

    有个message.properties,可以如下方式得到它,PropertiesPath是它里面一项
    /**
     * 
     * @param strKey
     * @return
     */
    public static String getPropertiesPath() {
        java.util.ResourceBundle rb = java.util.ResourceBundle.getBundle("com.mail.util.message");
        return rb.getString("PropertiesPath");
    }
      

  3.   

    用反射
    String thepath = (this.getClass()).getProtectionDomain().getCodeSource();
    这样就行了