making a jar file with multiple classes and jars

Sharky Forums


Results 1 to 8 of 8

Thread: making a jar file with multiple classes and jars

  1. #1
    Tiger Shark munchy1988's Avatar
    Join Date
    Sep 2002
    Location
    The Dirty Jerz
    Posts
    932

    making a jar file with multiple classes and jars

    I want to make a jar file that must also contain 2 jar files. is it possible to do this? and how would i go about doing it?

    also, i would like to include some picture files and somehow reference them in the jar file somehow. could someone tell me how i could do this?

    its been almost a year since i programmed in java, and it sucks relearning it all.
    Last edited by munchy1988; 03-28-2007 at 12:21 AM.
    Asus S96J | ATi Mobility Radeon X1600 | T2500 Core Duo | 2 GB DDR2 667 Mhz

  2. #2
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    Embedding picture files as resources isn't hard -- just embed them and then use the classpath within your application.

    As for embedding jar files, that's not usually done. Instead extract all the classes from the various jars and create one big one.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  3. #3
    Tiger Shark munchy1988's Avatar
    Join Date
    Sep 2002
    Location
    The Dirty Jerz
    Posts
    932
    Quote Originally Posted by rock
    Embedding picture files as resources isn't hard -- just embed them and then use the classpath within your application.

    As for embedding jar files, that's not usually done. Instead extract all the classes from the various jars and create one big one.

    i did realize i had to extract the class files first, but im still having difficulty with the images. in the app, i refer to the filename as "images/gold.gif". the file gold.gif is inside the images folder that i put in the jar file. however, when i run the jar file, the images do not work. i was reading that you somehow have to put it to a URL object, but i'm not sure how to do this, nor how to make it work. anyone else know how to make the images work aka easier ways than a URL object?
    Asus S96J | ATi Mobility Radeon X1600 | T2500 Core Duo | 2 GB DDR2 667 Mhz

  4. #4
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    Here's a little code snippet from some of my old code that should help:

    setIconImage(new ImageIcon(getClass().getResource("/com/blah/icon.gif")).getImage());

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  5. #5
    Tiger Shark munchy1988's Avatar
    Join Date
    Sep 2002
    Location
    The Dirty Jerz
    Posts
    932
    Quote Originally Posted by rock
    Here's a little code snippet from some of my old code that should help:

    setIconImage(new ImageIcon(getClass().getResource("/com/blah/icon.gif")).getImage());

    did you import anything for this to work?

    final Image gold = Toolkit.getDefaultToolkit().createImage("images/gold.gif").getScaledInstance(30,40,1);

    this is the code i had before. it works if the folder "images" is there with the pics inside of it. i put this folder in the jar file as well, but if the images folder isnt explicitly in the folder that the jar is in, the pictures wont work.
    Last edited by munchy1988; 03-29-2007 at 04:25 PM.
    Asus S96J | ATi Mobility Radeon X1600 | T2500 Core Duo | 2 GB DDR2 667 Mhz

  6. #6
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    The getClass().getResource() is the key part of finding the file within the jar.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  7. #7
    Tiger Shark munchy1988's Avatar
    Join Date
    Sep 2002
    Location
    The Dirty Jerz
    Posts
    932
    Quote Originally Posted by rock
    The getClass().getResource() is the key part of finding the file within the jar.

    i used that without using the .getImage() in the code. i tried

    final Image gold = Toolkit.getDefaultToolkit().createImage(getClass().getResour ce("/images/gold.gif")).getScaledInstance(30,40,1);

    its still not working

    any other ideas?
    Asus S96J | ATi Mobility Radeon X1600 | T2500 Core Duo | 2 GB DDR2 667 Mhz

  8. #8
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    Nothing is standing out. I was going to point out the missing leading "/" a couple posts above, but the last example has it. And I have to assume the "images" directory is at the root of the jar file, right?

    Can you debug the pieces and find out where it's failing? Something is probably coming back NULL that shouldn't be. Also - how is it not working? Just blank image or a NullPointerException?

    edit: I did find this: http://forum.java.sun.com/thread.jsp...sageID=4287812
    where there's a similar problem. The user seems to have issues, but there's a good debugging suggestion towards the bottom, printing out the URL and comparing it to the jar file.
    Last edited by rock; 03-30-2007 at 10:24 AM.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

Posting Permissions

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