This discussion is archived
11 Replies Latest reply: Jul 17, 2008 3:43 PM by 807589 RSS

getClass().getResource(String) to load readme.txt?

807589 Newbie
Currently Being Moderated
Hi. I'm sure some people had already had this problem, but I did a google search and didn't find a solution.
I got some code like this:
URL url = getClass().getResource("/res/readme.txt");
Scanner i = null;
try
{
     i = new Scanner(new FileInputStream(url.getPath()));
} catch (IOException e)
{
     JOptionPane.showMessageDialog(null, "Error");
}
StringBuffer o = new StringBuffer();
     while (i.hasNextLine())
          o.append(i.nextLine() + "\n");

String readme = o.toString();
This code works perfectly fine in Eclipse. But when I package it into a JAR file, it throws a NullPointerException and displays "Error". The rest of the program works fine. Also, getClass().getResource() works with images, whether inside a JAR or not. It's just with files that my code screws up.

Can someone please tell me what I'm doing wrong? Thanks.

Edited by: JavaGamer on Jul 3, 2008 2:49 PM
  • 1. Re: getClass().getResource(String) to load readme.txt?
    DrClap Expert
    Currently Being Moderated
    Then you aren't using a FileInputStream to read your images, if they are working correctly. Don't use one here either, because an entry in a jar archive isn't a File. Just use the getResourceAsStream method to get the InputStream to read from.
  • 2. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    Scanner i = new Scanner(getClass().getResourceAsStream("/res/readme.txt"));
              
    StringBuffer o = new StringBuffer();
    while (i.hasNextLine())
         o.append(i.nextLine() + "\n");
    
    String readme = o.toString();
    No this still doesn't work...
  • 3. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    Hi

    Anyone with a reply on this?

    I have read several post regarding this issue and never saw a relative reply.

    using getResource(".") works fine in eclipse but when I launch from a jar in a Windows enviromnment I get a null from getResource.

    So I used the name of the class that calls getResource("Data.class") and got a nice path but when I use ../../../ etc to reach the file within the jar, it doesnt work from the jar in Windows. Again within Eclipse it works fine.

    For example I get the following FileNotFound message:
    C:\DataGenerator.jar!\com\one\two\..\..\..\templates\templateA.xsl (The system cannot find the path specified)
    and within the jar (which is on c:\) there is a file templates\templateA.xsl

    Any ideas anyone?

    Thanks
  • 4. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    Vasilis wrote:

    Any ideas anyone?
    Yes you have made some mistakes. And this for starters looks very much like you are still doing exactly what you were told in reply #1 not to do but that you are continuing on anyway and whining that it doesn't work.
    C:\DataGenerator.jar!\com\one\two\..\..\..\templates\templateA.xsl (The system cannot find the path specified)
    I mean what's all that file nonsense about?

    Please post your updated formatted code, tell us the exact place the files are in the jar file and what errors you are getting. This stuff works. So the reason it's not working for you is that either in your code or in your jar packaging or maybe even both you have some errors. But I'm not interested, and neither should anyone else be, in playing 20 questions with you when what you say is happening and what you say you have changed do not jive with your output or what you were told.
  • 5. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    a) I am truly sorry. I didnt know you were on your period when I asked the question.

    b) I guess somewhere between your 11000 posts you lost your manners.

    c) I am not the person that asked the initial question.

    d) I understand the problem the person that started the thread has and I am sure he understands mine.

    e) If you dont feel like helping there is no need to post only to show that you are an ass.

    f) I dont whine, unlike your mum.

    g) I have stated where the class that calls getResource is and where is the file I am trying to reach, as well as the jar files name. If you do not see this I can't imagine what you have written in your 11000 posts. Possibly just bullying people.

    h) Take a chil pill, relax and then reply to posts.

    i) If you usually are not such an ass and just had a really bad day or something, then I forgive you for your post and I apologise for mine.

    Code is not even required since the right solution might be different than what we are trying to do.

    Within eclipse you can have other folders which reside on the same level as your classes so you just use a relative path like "foldername/filename.txt" and you reach the file you want. If you use that relative path, when the program is executed from within a jar, it checks to find the file relative to the point of execution of the file. So if your program is in C:\myjavaapp.jar if you say inside the app foldername/filename.txt is will try to get it from C:\foldername\filename.txt which is not very useful since foldername/filename.txt are inside the jar file and not on C:\

    So most people will think that it would be a good idea to get the location of a class that is running at the time and relative to that, reach your filename.txt file.

    Since foldername sits on a higher position in the path than the classes one might think its a good idea to use ../../ to go up a couple of levels. (Note: ../ means the level above the context level.)

    Unsuprisingly, this method seems to work fine within Eclipse but doesnt work from within a jar file.

    When you ask Java about the path of a class within a jar, it return a path that has the jar file with an ! at the end of its (the jars) filename. Although this is the returned path from Java, from what I understand, it cannot reach a file through this method.

    So any ideas how to have all your files within a jar, and being able to reach them?
  • 6. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    Why is it that people take any reason, any mistake, to try to start a flame war.

    Vasilis, no one is on their period, no one is a ass, and no one's mother whines. You spent far too much time trying to put down cotton.m for a simple mistake ... he thought you were the OP.

    Lighten up, dude.

    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  • 7. Re: getClass().getResource(String) to load readme.txt?
    DrClap Expert
    Currently Being Moderated
    I don't really feel like answering after reading that diatribe. But what the hell, why not? The answer is trivial.

    If you use a relative path in your URL in class.getResource(), you have to realize that it's relative to the class which is calling that method. Forget about Eclipse, that's irrelevant. If you use an absolute path (starting with "/") then that's the absolute path inside the jar. Again, don't let yourself get confused by where Eclipse likes to put things.
  • 8. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    DrClap thanks for the reply! Thats all I needed!

    So a relative path is relative to the location of the jar file and an absolute path points within the root of the jar.

    Also you probably know about it but I found a really nice eclipse plugin called Fat Jar that uses one-jar and packs everything (including other jars) nicely into a single jar.
  • 9. Re: getClass().getResource(String) to load readme.txt?
    800345 Newbie
    Currently Being Moderated
    Vasilis wrote:
    DrClap thanks for the reply! Thats all I needed!
    Not really. What you need is:
    a) To learn some forum manners so you don't hijack other people's threads
    b) To learn some respect and manners in general so you don't piss on the people who are here to help you
    c) Some brains to learn how to read the API
  • 10. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    stevejluke wrote:
    Vasilis wrote:
    DrClap thanks for the reply! Thats all I needed!
    Not really. What you need is:
    a) To learn some forum manners so you don't hijack other people's threads
    Courage -- to start a new thread, when needed.
    b) To learn some respect and manners in general so you don't piss on the people who are here to help you
    A heart - for compassion
    c) Some brains to learn how to read the API
    And a brain! We're off to see the wizard, ...
  • 11. Re: getClass().getResource(String) to load readme.txt?
    807589 Newbie
    Currently Being Moderated
    I happened to find that getResourceAsStream(String) solves the problem nicely, as a file inside a .jar does not exist. Any other media, such as images, sound, etc, can be loaded with getResource(String) without any problems. As the thread starter, I would like this thread to be closed.