譬如程序里有个1.jar的文件是本程序需要参考核对的内容
用fat jar打包进去以后 原本运行稳定的程序打包后找不到资源文件1.jar
想问问怎样打包文件,怎样的路径?
譬如resource/1.jar打包
但是File classDir = new File("resource/1.jar");
找不到这个1.jar

解决方案 »

  1.   

    InputStream in = this.getClass().getClassLoader()
    .getResourceAsStream("resource/1.jar");
    prop = new Properties(); prop.load(in);你的资源文件后缀竟然是.jar。。
      

  2.   

    首先谢谢楼上的同学~
    这个方法不是读属性文件配置文件的么
    我需要读1.jar里的class以及META-INF/MANIFEST.MF
    在程序里原本可以运行的代码是JarFile jarfile = new JarFile("resource/1.jar");
    如果读java文件夹的内容File file = new File("("resource/java"");
    看到网上说"不能使用物理路径, 只能使用相对于当前类文件的相对资源路径, 只有这样才能保证打包以后程序才能找到这些资源文件"
    那么我这个路径该怎样引用呢?
    谢谢
      

  3.   

    是要指定你要读取的文件名吧
    比如说要读取a.txt这个文件在F盘下,那么这个路径就是f:\\a.txt
    也就是File file = new File("f:\\a.txt");
      

  4.   

    写了两个简单的例子显示一个zip或者jar文件中包含的文件
    package squall.file;import java.io.FileInputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class ZipViewer
    {
    public static void main(String[] args) throws Exception{
    if(args.length != 1)
    {
    System.err.println("useage: java ZipViewer filename");
    System.exit(-1);
    }
    ZipInputStream zin = new ZipInputStream(new FileInputStream(args[0]));
    ZipEntry entry;
    while((entry = zin.getNextEntry()) != null){
    System.out.println(entry.getName());
    zin.closeEntry();
    }
    zin.close();
    }
    }从jar文件中读MANIFEST.MF文件
    package squall.file;import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class ReadMainFestInJar
    {
    public static void main(String[] args) throws Exception{
    if(args.length != 1)
    {
    System.err.println("useage: java ZipViewer filename");
    System.exit(-1);
    }
    ZipInputStream zin = new ZipInputStream(new FileInputStream(args[0]));
    ZipEntry entry;
    while((entry = zin.getNextEntry()) != null){
    if("META-INF/MANIFEST.MF".equals(entry.getName()))
    {
    BufferedReader in = new BufferedReader(new InputStreamReader(zin));
    String line;
    while((line = in.readLine()) != null)
    {
    System.out.println(line);
    }
    }
    zin.closeEntry();
    }
    zin.close();
    }
    }
      

  5.   

    我写的绝对路径File classDir = new File("E:/1.jar");去读是读的到的,但是直接用这个方法是不是读不到打包到JAR包里的这个1.jar
    我觉得是路径的问题,但不是解决问题的根本 
    问题的根本是这个方法得不能打开1.jar
    不知道我这么想对吗
    请老大们指点
    解决了另开贴加分!
      

  6.   

    建议你把这个文件的位置做为jvm的一个参数。因为你
    File classDir = new File("resource/1.jar");这样写的话,在不同的运行环境中很有可能是不同的目录。
      

  7.   

    果然在jar包里面,不能通过普通的访问文件的方法访问  URL urlJar = this.getClass().getClassLoader().getSystemResource(
              "ciku.Pat");
          String urlStr = urlJar.toString();
          int from = "jar:file:/".length();
          int to = urlStr.indexOf("!/");
          urlStr = urlStr.substring(from, to);
          File classDir = new File(urlStr);
    如此仍然未解决问题,继续搜索求助中!
      

  8.   

    谢谢Squall1009,我在classpath中加了资源文件的路径仍然不行
      

  9.   

    我的意思不是在classpath中加这个昏迷。package squall.test;public class GetProperties
    {
    public static void main(String[] args)
    {
    System.out.println(System.getProperty("configfile.path")); }
    }把这个类文件编译好了以后,用java 命令的-Dconfigfile.path=你的jar文件的物理位置传递给程序
    java -Dconfigfile.path=C:\1.jar squall.test.GetProperties
    这样上面的程序System.getProperty("configfile.path")出来的String就是C:\1.jar了
      

  10.   

    yi?为什么要用物理路径,不明白了,请高人讲讲 我如果用物理路径直接File classDir = new File("c:/1.jar");就可以读的到的啊,打包之后也读的到啊,但是现在是这个资源文件打包到JAR包里,即使用物理路径也不能这么读文件,简单的File不能访问到.
      

  11.   

    说错了 =.=!
    怎么不能改奥 重说
    yi?为什么要用物理路径,不明白了,请高人讲讲 我如果用物理路径直接File classDir = new File("c:/1.jar");就可以读的到的啊,打包之后也读的到啊,但是现在是这个资源文件打包到JAR包里,要读JAR包里的文件即使用相对路径也不能这么读文件,简单的File不能访问到.
    使用物理路径就是读JAR包外的资源了.
      

  12.   

    应该是类似Squall1009说的从jar文件中读MANIFEST.MF文件的方法
    只不过我读的是文件夹的内容还有一个JAR包
      

  13.   

    getClass().getResource("/resource/icon1.png");
    像这种格式 读取 jar 文件中的资源,它得到的是 资源文件 在jar 文件中的相对路径。
    这样 在本包内的资源文件 就可以用了。