Results 1 to 15 of 15
  1. #1
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Creating JAR archieve

    hello everyone,
    I wish to create one JAR archive for my application.
    This application uses frame and panels etc and other swing components.
    I had added images to panel.

    The problem is that when i created JAR archive as:

    "jar cvfm converter.jar manifest.txt *.class resources" (where "resources" is folder where i placed my images),

    it successfully got created,but when i delete those original images from my harddisk,the jar application is unable to display images though i had included it in jar archive ,
    Please help me out so that i can run my JAR on any computer.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Creating JAR archieve

    What code do you use for accessing the images?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Creating JAR archieve

    I am using the following code:

    Java Code:
    public void paintComponent(Graphics g) 
    {
    super.paintComponent(g);
    
    Image a=Toolkit.getDefaultToolkit().getImage("F:\\javahim\\converter\\resources\\area.jpg");
    g.drawImage(a,0,0,getSize().width,getSize().height,this);
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Creating JAR archieve

    You've given it the exact directory path to the file on the disk.
    So when you delete the file on the disk it doesn't find it.

    You need to use getResource or getResourceAsStream.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Creating JAR archieve

    dont know how to use these methods.Can you help with some information.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Creating JAR archieve

    Class.getResource(<path>)
    where <path> is the path to the resource relative to the class used, or relative to the "top" of the jar file if the path begins with a '/'.
    So:
    Java Code:
    URL url = <YourClass>.getResource("/somedirectory/someresource.gif");
    will return a url that you can use in createImage, assuming there is a someresource.gif in the somedirectory bit of the jar file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Creating JAR archieve

    When I put my class name in <YourClass> field,it gives an error .

    I use getClass() method then the url returns null.

    whats the problem here.And also my this class does not contain main() method.

    Java Code:
    URL url=getClass().getResource("F:\\javahim\\converter\\New folder\\New folder\\resources\\area.jpg");
    Image a=Toolkit.getDefaultToolkit().getImage(url);
    g.drawImage(a,0,0,getSize().width,getSize().height,this);
    .help me out.please

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Creating JAR archieve

    Whoops, sorry, should have been <YourClass>.class.getResource...

    And the path used is the path to the resource *in the jar*.
    It has nothing to do with the file syetem.
    Also it uses '/' as the directory separator.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Creating JAR archieve

    unable to get you.

    Can you please show the code.
    my class name is "areapanel"

  10. #10
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Creating JAR archieve

    Replace <YourClass> with your class name. In this case it will:

    Java Code:
    areapanel.class.getResource(...);
    It is better to name you class AreaPanel instead of areapanel. Let's follow the Java code convention when writing our code.

  11. #11
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Creating JAR archieve

    when I placed the image file parallel to class file I get correct output.

    Java Code:
     
    URL url=areapanel.class.getResource("area.jpg");

    But when I place the image file in some folder ,It gives null.as

    Java Code:
    URL url=areapanel.class.getResource("F:/javahim/converter/New folder/New folder/resources/area.jpg");
    System.out.println("URL="+url);
    please help .Thanks for your previous replies.

  12. #12
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Creating JAR archieve

    Wasn't your original question was how to read a resource located inside a Jar file? If you want to read whether the resource is inside a jar or on a directory just pass an absolute path to the root application folder. For example if you put your image in resource directory (whether packaged in jar or in you hard drive) you can do it like:

    Java Code:
    AareaPanel.class.getResource("/resource/area.jpg");
    If your resource is not package in Jar the directory should be in the root directory of your application.

  13. #13
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Creating JAR archieve

    many thanks to you.Problem finally gets solved.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Creating JAR archieve

    But do you understand why?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  15. #15
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Creating JAR archieve

    As far as I understand,this is because initially I was supplying an absolute path which might get changed and will change on different computer.
    But now here a relative path is set which is always constant in our JAR archive.

    But if someone make me understand more with depth, it will be great.
    Thanks to all members.

Similar Threads

  1. Creating an App
    By Joao Lourenco in forum New To Java
    Replies: 2
    Last Post: 09-20-2012, 12:29 AM
  2. Replies: 4
    Last Post: 04-02-2012, 02:50 PM
  3. Replies: 9
    Last Post: 07-29-2011, 03:18 PM
  4. Creating files stopped creating...
    By Dieter in forum Advanced Java
    Replies: 3
    Last Post: 09-26-2009, 12:45 AM
  5. Archieve tehnologies
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 09-18-2009, 03:14 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
  •