Results 1 to 14 of 14
  1. #1
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Loading Resources

    Hello everyone, I've been a hell of time with this last problem. I don't understand how to load resources in netbeans since normally I don't use netbeans at all. I made a package and put some text files and an image to be loaded during runtime. Problem is that I can only access my resources while running netbeans but not as a standalone executable jar. Here is my code and a picture to show you my package hierarchy:
    Loading Resources-addingresources.png
    Java Code:
    Properties props = new Properties();
    try (InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("resources/gmail.txt"))
    {
            props.load(in);
    }
    List<String> lines = Files.readAllLines(Paths.get(ClassLoader.getSystemClassLoader().getResource("resources/msg.txt").toURI()), Charset.forName("UTF-8"));
    
    Image img = new ImageIcon(PreferencesFrameFactory.class.getResource("/resources/mail.png")).getImage();
    I know I'm getting a fat exception in my jar that propagates to the rest of my program. I don't understand what the working directory of the jar will be and how to obtain the actual resource paths (not streams alone). Overall I'm extremely confused.
    Also if someone could tell me how to add a resource file that won't be bundled in the the jar that would be great also.

    For those curious my first two resource loading statements are for JavaMail and my last resource loading statement is for am ImageIcon for the system tray.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Loading Resources

    What are the paths to the resources when they are in the jar file?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Loading Resources

    I would assume it would be root of the jar (/) resources (/resources/) file (something.txt) since its an archive file format?
    Sorry, I fail to understand exactly what your asking as that's what I've tried to implement as shown behavior above.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Loading Resources

    Look in the jar file with a utility program that can read zip files and see where the resource files are located.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Loading Resources

    Ok, great getting somewhere now. I had hunch this might be the path but I wasn't quite sure: D:\NetBeans 7.4\projects\DragNDropMail\dist\DragNDropMail.jar\ resources\. Now how do I go from knowing this path to using the ClassLoader to obtain this path.
    I've tried debugging what I've retrieved in previous attempts and I keep get a cacophony of !'s in the path.

    Here is my current assumption:
    ClassLoader.getSystemClassLoader().getResource("re sources/msg.txt").toURI() <--- this is returning a null because it is searching DragNDropMail.jar\com\myName\DragNDropMail\ inside the jar rather than DragNDropMail.jar\resources\.
    I don't understand how to obtain a working directory from which to obtain relative folders inside a jar. In netbeans, I used File --> Project Properties --> Run --> Working Directory. In a terminal environment I run the program from the working directory of my choice. Where should I be looking for obtaining this? am I even on the right path?

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Loading Resources

    I'm confused. Where are the resource files located? In the jar file or on disk?
    Have you looked inside of the jar file? What was the path to the files you are trying to read?

    There is no working directory inside a jar file. The "root" of the folders in a jar file are considered to be on the classpath where the getResource methods look.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Loading Resources

    Ok, I'm using the zip file utility 7zip after building the project.
    After going to Run --> Clean and Build Project,
    I obtained 1 jar containing my source package. This contains
    com\myName\DragNDropMail\ <--- all the class files are then located in this directory
    META-INF <--- containing the manifest
    resources\ <--- containing the image and text files
    A lib directory containing the JavaMail jar.

    From the 7zip I state that the path is D:\NetBeans 7.4\projects\DragNDropMail\dist\DragNDropMail.jar\ resources\ because according to 7zip this is where the text files and imagse are located.

    I'm very confused when you ask "Where are the resource files located? In the jar file or on disk?"
    I thought that the whole point of a jar is to package the .class files and any other files inside an archive for distribution. Therefore I would assume that the resource files are in the jar file since you need to make the archive independent of the disk. As for my resources before making a jar file, they are on the disk at D:\NetBeans 7.4\projects\DragNDropMail\src\resources\

    "What was the path to the files you are trying to read?"
    Whichever path leads to the resources independently of the disk so that I can distribute my jar without handing out a project folder. I would assume this is some path within the jar.

    Also I'd like to say that I have set no classpath in case this attributes to the reason I can't find the .txt files and image file.
    Last edited by typedef; 03-04-2014 at 04:50 AM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Loading Resources

    Where the jar file is located is not related to how the getResource methods obtain files from inside of the jar file.
    If the desired files are in the resources folder inside the jar file, then the code should work if the spelling of the path and files is correct.

    I would assume
    It is better to verify by looking in the jar file than to assume a file is in it.

    resources\ <--- containing the image and text files
    What were the full paths and spellings for those files?

    I know I'm getting a fat exception
    Please copy the full text of the error messages and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Loading Resources

    I do not understand what your asking.
    It is better to verify by looking in the jar file than to assume a file is in it.
    You have already asked me to verify. I have. The path I gave: "D:\NetBeans 7.4\projects\DragNDropMail\dist\DragNDropMail.jar\ resources\". The three text files and the image are within this directory.

    This is of course an absolute path given by the a zip program you asked me to clarify here:
    What was the path to the files you are trying to read?
    As to whether this is the Path structure required to traverse a jar to obtain a path within in the jar I have no idea. This is why I have asked in the question.

    As for the exception it's a null pointer exception - because the directory is not being found.
    I have listed the paths to all my files, both the jar path and the netbeans src path. Could you please recommend an object to use within the context of my code to be able to obtain the Path for the resources in the jar file so that my program may use the resource outside of just netbeans. I am asking for some direction - not a handout. A code snippet or a link on how to manage a Path within the relative context of a jar archive would be greatly appreciated.

    Thank you for your time.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default Re: Loading Resources

    You can consider a .jar file as a sort of 'root' point, i.e. if a .jar file contains the files /foo/x.text and /foo/bar/y.png, the getResource(AsStream)( ... ) methods find the entries as "/foo/x.txt" and "foo/bar/y.png"; now suppose you also have a class file foo.MyClass, i.e. it is stored in 'directory' foo in that .jar file; the foo.MyClass.getResource(AsStream)( ... ) methods will find the same files if pointed to, relative to the class file, as "x.txt" and "bar.y.png".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Loading Resources

    I know I'm getting a fat exception
    Please copy the full text of the error messages and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Loading Resources

    Alright, so I rearranged the code as such and changed my resources directory for the current moment until I understand more about loading relative resources. I'm thinking that the reason the code kept getting a null pointer exception was because it was trying to get a resource from a path with exclamation marks in it: D:/NetBeans%207.4/projects/DragNDropMail/dist/DragNDropMail.jar!/com/alex/DragNDropMail/resources/gmail.txt
    I'm assuming it works now because the resource is further down the hierarchy relative to the class file, no need to use code to find out the absolute path of the file/archive system. I'm still really confused and might well be seriously babbling. Now I'm clean up the code from my entire project and look further into obtaining resources/paths from a jar file.
    Thank you both for trying to help me.

    Loading Resources-addingresources.png

    Java Code:
    try (InputStream in = getClass().getResourceAsStream("resources/gmail.txt"))
    {
        props.load(in);
    }
    Scanner in = new Scanner(getClass().getResourceAsStream("resources/msg.txt"));
    ArrayList<String> lines = new ArrayList<>();
    while (in.hasNextLine())
        lines.add(in.nextLine());
    
    Image img = new ImageIcon(PreferencesFrameFactory.class.getResource("resources/mail.png")).getImage();

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: Loading Resources

    Does the code work now?
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

Similar Threads

  1. Loading images/resources from a jar.
    By verd in forum Java Applets
    Replies: 6
    Last Post: 07-30-2012, 12:03 AM
  2. loading resources in an applet
    By Gatts79 in forum Java Applets
    Replies: 5
    Last Post: 03-06-2009, 03:19 PM
  3. Protocol for loading resources from jar
    By SamSam in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2009, 08:14 PM
  4. Loading resources generates warning messages
    By Namita Patil in forum New To Java
    Replies: 6
    Last Post: 07-03-2008, 03:04 PM
  5. Problem loading resources.
    By jimm1 in forum Advanced Java
    Replies: 6
    Last Post: 06-23-2008, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •