Re:Read file in the jar file?

Discussion in 'Java' started by pvii007, Dec 9, 2005.

  1. pvii007

    pvii007 Guest

    import java.io.*;
    public class A
    {
    public static void main( String[] args)throws Exception
    {
    A app = new A();
    BufferedReader bf = new BufferedReader(
    new InputStreamReader(app.getResourceAsStream("a.txt"))
    );
    if( bf.ready())System.out.println(br.readLine());
    }
    }
    a.txt
    hello world

    I compress the compiled A and a.txt in the jar file.

    Run it. It's successful.

    But
    import java.io.*;
    public class A
    {
    public static void main( String[] args)throws Exception
    {
    A app = new A();
    BufferedReader bf = new BufferedReader(
    new InputStreamReader(app.getResourceAsStream("/dir/a.txt"))// here I
    modify it
    );
    if( bf.ready())System.out.println(br.readLine());
    }
    }
    dir/a.txt
    hello world

    I still create the jar file.
    Run it. It's failure

    Why?

    Is reading some file in the jar file approprate for the situation that the
    file and the class is under the same package ?

    If I want to use the class to read the file which is not under same package,
    how to implement it?
     
    pvii007, Dec 9, 2005
    #1
    1. Advertising

  2. Re: Read file in the jar file?

    Try this instead :
    app.getResourceAsStream("dir/a.txt")

    without the root-slash

    Paul Hamaker
    SEMM
    http://javalessons.com
     
    Paulus de Boska, Dec 9, 2005
    #2
    1. Advertising

  3. pvii007

    Alex Guest

    Re: Read file in the jar file?

    My 2 cents.
    1) / is root of jar file. So, if it's in dir "dir" in jar it will be
    OK.
    2) I dont't know why but you can't read files as resources from root
    directly.
    They must be in some directory.

    So, create jar with at least one directory "dir", place file here and
    read it as
    /dir/a.txt

    It works.
     
    Alex, Dec 9, 2005
    #3
  4. pvii007

    pvii007 Guest

    Re: Read file in the jar file?

    import java.io.*;
    public class A
    {
    public static void main( String[] args)throws Exception
    {
    A app = new A();
    BufferedReader bf = new BufferedReader(
    new InputStreamReader(app.getClass().getResourceAsStream("txt/b.txt"))
    );
    if( bf.ready())System.out.println(bf.readLine());
    }
    }

    Sorry It's still failure

    E:\code\java\test\jar\test>java -jar a.jar
    Exception in thread "main" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at A.main(A.java:7)

    "Alex" <>
    ??????:...
    > My 2 cents.
    > 1) / is root of jar file. So, if it's in dir "dir" in jar it will be
    > OK.
    > 2) I dont't know why but you can't read files as resources from root
    > directly.
    > They must be in some directory.
    >
    > So, create jar with at least one directory "dir", place file here and
    > read it as
    > /dir/a.txt
    >
    > It works.
    >
     
    pvii007, Dec 10, 2005
    #4
  5. pvii007

    Roedy Green Guest

    Re: Read file in the jar file?

    On Sat, 10 Dec 2005 10:27:46 +0800, "pvii007" <>
    wrote, quoted or indirectly quoted someone who said :

    >E:\code\java\test\jar\test>java -jar a.jar
    >Exception in thread "main" java.lang.NullPointerException
    > at java.io.Reader.<init>(Unknown Source)
    > at java.io.InputStreamReader.<init>(Unknown Source)
    > at A.main(A.java:7)


    lets see a directory of your jar to make sure you have everything
    named correctly.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 10, 2005
    #5
  6. pvii007

    Alex Guest

    Re: Read file in the jar file?

    You didn't include / !!!
    It means that directory txt msut be where you class is.
    For example if it is class abc.xyz.Test
    then directory and file should be (in the jar) /abc/xyz/txt/b.txt

    So I suggest always use root and have it like
    /config/b.txt
     
    Alex, Dec 10, 2005
    #6
  7. pvii007

    pvii007 Guest

    Re: Read file in the jar file?

    That's OK.
    Thank you for your help.

    Another problem.

    If I have a file structure
    /core/ (here is application)
    /core/doc ( here is the doc )
    /core/doc/filecollection1/
    /core/doc/filecollection1/subcollection1/
    /core/doc/filecollection1/subcollection2/
    /core/doc/filecollection2/
    /core/doc/filecollection2/subcollection1/
    /core/doc/filecollection2/subcollection2/

    I compress the file and doc in the jar file.I want to access to every dir or
    file recursively.

    How to write my code?

    PS:
    I have written the code
    ....

    File codebase = new File("/core/doc");
    ....
    public String read_file_recursive( File file)
    {
    String ret;
    if( codebase.isDirectory())
    {
    File[] filelist = codebase.listFiles();
    for( int i = 0 ; i < filelist;i++ )
    {
    ret += read_file_recursive(filelist);
    }
    }
    else
    {
    // read file content ...
    }
    return ret;
    }
    ....
    But it doesn't work under the jar file.



    "pvii007" <> дÈëÏûÏ¢ÐÂÎÅ:dndef5$r8g$...
    > import java.io.*;
    > public class A
    > {
    > public static void main( String[] args)throws Exception
    > {
    > A app = new A();
    > BufferedReader bf = new BufferedReader(
    > new InputStreamReader(app.getClass().getResourceAsStream("txt/b.txt"))
    > );
    > if( bf.ready())System.out.println(bf.readLine());
    > }
    > }
    >
    > Sorry It's still failure
    >
    > E:\code\java\test\jar\test>java -jar a.jar
    > Exception in thread "main" java.lang.NullPointerException
    > at java.io.Reader.<init>(Unknown Source)
    > at java.io.InputStreamReader.<init>(Unknown Source)
    > at A.main(A.java:7)
    >
    > "Alex" <>
    > ??????:...
    >> My 2 cents.
    >> 1) / is root of jar file. So, if it's in dir "dir" in jar it will be
    >> OK.
    >> 2) I dont't know why but you can't read files as resources from root
    >> directly.
    >> They must be in some directory.
    >>
    >> So, create jar with at least one directory "dir", place file here and
    >> read it as
    >> /dir/a.txt
    >>
    >> It works.
    >>

    >
    >
     
    pvii007, Dec 10, 2005
    #7
  8. pvii007

    Roedy Green Guest

    Re: Read file in the jar file?

    On Sat, 10 Dec 2005 12:40:07 +0800, "pvii007" <>
    wrote, quoted or indirectly quoted someone who said :

    >But it doesn't work under the jar file.


    If you wanted it to look inside jar files, you would need to use a
    filter to find the *.jar files, and individually read them with the
    jar classes. Members are not part of the file system.

    See http://mindprod.com/jgloss/jar.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 10, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lucy
    Replies:
    6
    Views:
    30,903
    steve
    Sep 7, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    614
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,767
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,847
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    690
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page