如题

解决方案 »

  1.   

    如果是个不变的文件,加到jar文件里;
    也可以把这个目录加入classpath
    然后使用getClass().getClassLoader().getResource(...)/getResourceAsStream(...)
      

  2.   

    比如说Linux一目录下有个jar包和一个properties文件,jar里的类需要解析properties文件,而properties文件可以被修改。
      

  3.   

    引用的那个JAR包里的manifest添加Class-Path,后面跟上要引用的JAR包名,那么在使用时自动会引用这个JAR包进ClassPath,而properties文件的引用参考Java运行时的当前运行路径获得方式。getClass().getClassLoader().getResource(...)/getResourceAsStream(...)
    该方法可以读取JAR包内的文件。
      

  4.   

    能不能写个详细的用法,我试过
    String path = ManagePage.class.getProtectionDomain().getCodeSource().getLocation().getPath();得到的是file:c:\documents and settings\...\local settings\..,而不是jar的路径
      

  5.   

    你现在碰到的到底什么问题?properties文件读取路径确定不了?还是要引用的JAR引用不到?
      

  6.   

    我的问题是“properties文件读取路径确定不了”,希望能帮忙解决,很急。
      

  7.   


    System.out.println(YourClass.class.getProtectionDomain().getCodeSource().getLocation());
      

  8.   

    this.getClass().getResourceAsStream("/xxx.properties"),这样就可以了,谢谢大家。
      

  9.   

    URL url = TestClass.class.getProtectionDomain().getCodeSource().getLocation();
    得到jar包的所在的绝对路径得到中文路径的String字符串。
                    //这段代码我试过能用,但有待验证
            // 将url路径转码,主要应用于汉字
    String temp = "";
    try {
    temp = URLDecoder.decode(url.getFile(), "UTF-8");
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    }
      

  10.   


            //得到jar包的位置。比如:c:\test.jar
    public static String getJREPath(){
    URL url;
    // 得到jar程序的路径
    url = FileUtil.class.getProtectionDomain().getCodeSource().getLocation();
    // 将url路径转码,主要应用于汉字
    String temp = "";
    try {
    temp = URLDecoder.decode(url.getFile(), "UTF-8");
    } catch (UnsupportedEncodingException e1) {
    e1.printStackTrace();
    }
    return temp;
    }
            //得到jar同目录的文件路径,参数是文件的名称
    public static String getJARPathFile(String fileName){
    return new File(getJREPath()).getParent()+File.separator+fileName;
    }