This discussion is archived
8 Replies Latest reply: Jan 16, 2007 11:28 AM by 807599 RSS

Jar cannot find text file?

807598 Newbie
Currently Being Moderated
Hi all,

I've got a jar and thought that it was working fine. It has all the files that I want in it and I thought that it was running ok. I cut and paste the jar onto my desktop and run it from there and it cannot find a text file used to read in server connection details. It's there in my jar file. What could be wrong?

Regards,

Chris
  • 1. Re: Jar cannot find text file?
    807598 Newbie
    Currently Being Moderated
    Are you sure it was ever actually reading the text file from your JAR? Maybe it was reading it from the directory the JAR used to be in and it just so happens that the text file is in your JAR as well.
  • 2. Re: Jar cannot find text file?
    807598 Newbie
    Currently Being Moderated
    Well that's what appears to have happened because when I put it back in the orignal directory, with serverFile.txt in there it works. Is it perhaps the way I read it in?
    BufferedReader in = new BufferedReader(new FileReader("serverFile.txt));
    Cheers,

    Chris
  • 3. Re: Jar cannot find text file?
    807598 Newbie
    Currently Being Moderated
    good .. you reached the solution urself with the hint :D .. it's always better I feel to reach the root cause on your own rather than spoon feed .. a clue here n there is fine :D
  • 4. Re: Jar cannot find text file?
    807598 Newbie
    Currently Being Moderated
    OK so I now have:-
    InputStream is = getClass().getClassLoader().getResourceAsStream(serverFile);
                   BufferedReader in = new BufferedReader(new InputStreamReader(is));
    which works nicely.

    Now a second problem. I also add contacts to a contacts.txt file also located in the jar. If I run the jar it creates a new file in the same directory as the jar file is located. I need it to write to the contacts.txt file in the jar so each time I connect, all contacts are displayed.
    I'm using RandomAccessFile to locate the end of the file so I can add the contact to the end of the file instead of overwriting stuff. However I'm not sure how to go about it.

    I have:-
    File file = new File(contactListFile); //create the file object pointing to contacts list
                      long fileLength = file.length(); //get file length
                      RandomAccessFile raf = new RandomAccessFile(file, "rw"); //create random access file
                      raf.seek(fileLength); //find where end of file is
                      raf.writeBytes(selectedContactAll + "\r\n");//add contact to end of file
                      raf.close();//close the RandomAccessFile
    I understand that I can create a File object by having a URI and that I can get a URL of the location using the getResource() method of ClassLoader. Is there anyway of converting URL to URI or is there a completely better solution?

    Chris
  • 5. Re: Jar cannot find text file?
    807598 Newbie
    Currently Being Moderated
    Ok so I should have known that there was a toURI() method. I now have
    URL myURL = getClass().getClassLoader().getResource(contactListFile);
                        File file = new File(myURL.toURI());
                        //File file = new File(contactListFile); //create the file object pointing to contacts list
                      long fileLength = file.length(); //get file length
                      RandomAccessFile raf = new RandomAccessFile(file, "rw"); //create random access file
                      raf.seek(fileLength); //find where end of file is
                      raf.writeBytes(selectedContactAll + "\r\n");//add contact to end of file
                      raf.close();//close the RandomAccessFile
    I get FileNotFoundException even though it is in the jar?
  • 6. Re: Jar cannot find text file?
    DrClap Expert
    Currently Being Moderated
    I get FileNotFoundException even though it is in the jar?
    The thing may be in the jar, but it is not a FILE. So stop trying to use java.io.File methods to access it. You're on the right track with that getResource() method, you'll find there's also a getResourceAsStream() method that allows you to read from a jar entry.

    But do I detect that you are trying to update that jar entry? Forget it, you can't do that.
  • 7. Re: Jar cannot find text file?
    807598 Newbie
    Currently Being Moderated
    Hi Doc,

    Sorry I thought I made it clear that I can now read from a file in a jar. After trawling the web I've found that you can't write to a file in a jar as a jar is a file in itself, not a directory. Bit of a pain in the butt. Just have to store contact details on a database then.

    Regards,

    Chris
  • 8. Re: Jar cannot find text file?
    807599 Newbie
    Currently Being Moderated
    Actually, there is a way to output files into a jar, but I think it cannot be done if the jar is running.(meaning you would have to have 2 jars)