Results 1 to 17 of 17
  1. #1
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default [Solved] Embed ressources in JAR and load them

    How do I embed a ressource in a JAR and load it from the code from the JAR?

    The first part is easy, I believe, with Eclipse: Export JAR, include my ressource (a PNG image) and ready.

    But how do I access this from the code?

    (e.g. loading an icon with "icon = new ImageIcon("yadda.png", "My PNG from a JAR");")
    Last edited by MasterD; 04-01-2009 at 11:50 AM. Reason: Solved :)

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    you would have to

    A: put all images and code in the JAR file

    or

    B: unzip the JAR file. I believe JAR files can be unzipped allowing the contents to be edited and used outside the JAR file.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    As I was saying

    JAR resources should be accessed with codes like
    Java Code:
    icon = new ImageIcon(anchor.class.getResourceAsStream("yadda.png"));
    You know I mean )))

    Or maybe you want some like BufferedReader ? Report

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You can use a URL as well. First create the URL to the jar file as follows.

    Java Code:
    URL imgURL = [B]your_jar_file[/B].class.getResource("logo.gif");
    Then initialize the image as follows.

    Java Code:
    Image img = Toolkit.getDefaultToolkit().getImage(imgURL);
    You have to import the jar file into your project before doing this.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    URL imgURL = your_jar_file.class.getResource("logo.gif");
    Don't you mean:

    Java Code:
    URL imgURL = YourClass.class.getResource("logo.gif");
    You could try and build the image url from that of the jar file, but it's not obvious how you would get the url of the jar file. (And the resulting code would break if it were deployed some other way.)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    It is no good using URL if the JAR file is for an Applet. But author doesn't comment the thing :(

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, I agreed with you Webuser. Just a suggestion. ;) Better that our thread starter workaround it and see.

  9. #9
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default

    Thanks for all the reply!

    But please be patient with me, as my day starts later :)

    The App is a Java SE Desktop application, so no Applet.

    I tried it using the URL u = MyClass.class.getRessource(file) command, but the URL is always null.

    Is it important where the file resides within the JAR? (Well, I believe so:))

    Currently it is in the root of the JAR (when you open it with a zip programm). So when the file I have is "myicon.png" and resides in the root of the JAR, the loading with file="myicon.png" should be possible?

    In addition, which might be involved with my URL being null:
    You have to import the jar file into your project before doing this.
    Thats a thing I cannot do, I believe. The JAR files are plugins to my app, so they are not addable to my project, as they don't exist yet. The reason why I want to embed the resource into the JAR is that I don't want to have a installer needed to put the resources, the plugin needs, into some subfolder of the main app.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In NetBeans you can import external JAR files into projects. I hope Eclipse will do that too. Or else you can added the jar file in the classpath and refer it.

  11. #11
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default

    I found my error, and of course it is one which was solved by RTFM:

    getRessource locates the ressource somewhere in the package of the loading class.

    So, when I have
    net.example.MyClass
    And want to load using MyClass.class.getRessource("myicon.png"), this image must be located inside of the jar at
    net/example/myicon.png
    (So, alongside with the MyClass.class)
    Last edited by MasterD; 04-01-2009 at 11:27 AM.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    That's obvious right, I mean you must locate the resource is right place before using it. JAR file consist a folder structure.

  13. #13
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Did you make it or not?
    When I write
    Java Code:
    new ImageIcon(anchor.class.getResourceAsStream("yadda.png"));
    it means that your yadda.png can be in any folder of your jar packages tree. the anchor it is an objective expression
    For example
    1) you have package A
    2) package A contains class foo and yadda.png file
    3) So foo is the anchor :)
    to get yadda.png you write
    Java Code:
    ImageIcon icon = new ImageIcon(A.foo.class.getResourceAsStream("yadda.png"));
    It is easy to see )

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Are you directing on me webuser? As I said in one of my previous post, I advice to use the jar package. Same thing happen there as well, need to work-on with the complete path.

  15. #15
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Are you directing on me webuser? As I said in one of my previous post, I advice to use the jar package. Same thing happen there as well, need to work-on with the complete path.
    No, I am just asking... did the author succeed to get the resource?

  16. #16
    MasterD is offline Member
    Join Date
    Mar 2009
    Location
    Germany
    Posts
    22
    Rep Power
    0

    Default

    Yes, with the way I described I had success.
    However, I'm still a bit curious (this goes behond what I currently need):
    Is it possible to have a relative path in the getRessourece part?

    Just imagine I have a ressource that is used by several packages (independently and dynamically loaded, if you want). So I want just a single ressource file. With the following layout:

    Java Code:
    + Jar-file
    |-> foo
       |-> myicon.png
       |-> bar
          |-> Barrr.class
       |-> rab
          |-> Rrrab.class
    Would I need a emtpy class inside foo to access myicon.png, or could I write
    Java Code:
    foo.bar.Barrr.class.getResource("../yadda.png");

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You could try out this "relative" naming. Here is a class that attempts to find know itself: ie to find its own class file/url:

    Java Code:
    package foo;
    
    import java.net.URL;
    
    public class ResourceEg {
        public static void main(String args[]) {
            URL url = foo.ResourceEg.class.getResource("../foo/ResourceEg.class");
            System.out.println("The url is ... " + url);
        }
    }
    You put it in a directory called foo, compile and run. And compare that with running the same program when it is within a jar file:

    pbrockway@lind6off:~/Desktop$ javac foo/ResourceEg.java
    pbrockway@lind6off:~/Desktop$ java foo.ResourceEg
    The url is ... file:/home/pbrockway/Desktop/foo/ResourceEg.class
    pbrockway@lind6off:~/Desktop$ jar -cf test.jar foo
    pbrockway@lind6off:~/Desktop$ java -cp test.jar foo.ResourceEg
    ???
    pbrockway@lind6off:~/Desktop$
    The other thing to do (sorry if you've already done so) is to consult the getResource() API documentation.
    Last edited by pbrockway2; 04-06-2009 at 11:04 PM.

Similar Threads

  1. Have to embed link to website in JFrame
    By pkumar85 in forum New To Java
    Replies: 0
    Last Post: 11-24-2008, 12:01 AM
  2. Help, Embed video in java applet?
    By darthbane13 in forum New To Java
    Replies: 0
    Last Post: 08-29-2008, 10:09 PM
  3. How to embed Swing and AWT in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 08:07 PM
  4. How Do I Embed Java Correctly To A Web Page
    By abcd in forum Java Applets
    Replies: 7
    Last Post: 01-28-2008, 07:53 AM
  5. Email Embed Image
    By gui.gomes in forum Advanced Java
    Replies: 3
    Last Post: 11-19-2007, 02:08 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
  •