Results 1 to 14 of 14
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Images in JAR File (via Eclipse)

    Hey, I'm using Eclipse. I've written the first portion of my latest program, which uses images in a source folder called "img". When I run it in Eclipse, the images show up fine. However, when I try to execute the JAR file they do not show up. Perhaps I'm having trouble understanding exactly how I'm suppose to export them.

    I searched for help on sun's website ( New To Java - Re: How can I pack images into a runnable jar file in Eclipse? ), and tried to follow the help provided there. However, nothing I did seemed to work. Can someone try to explain what I need to do? Help appreciated.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Make sure that that directory actaully exists in the Jar and ensure that you are using getResource properly (and you won't be able to "File" in any way shape or form).

    Edit: It would help if you posted the code used to "load" the images.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    I don't know anything about getResource, so I assume that's my problem.

    Java Code:
    //Example Image use
    
    import javax.swing.ImageIcon;
    
    public class Grass extends AreaComponent
    {
    	public Grass()
    	{
    		super(
    		new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), 
    		new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), 
    		false);
    	}
    }

    Would this be the proper way to go about doing this?

    Java Code:
    //Example Image use
    
    import javax.swing.ImageIcon;
    
    public class Grass extends AreaComponent
    {
    	public Grass()
    	{
    		super(
    		new ImageIcon(getResource("img\\AreaComponent\\Grass\\1.gif").getImage(),
    		new ImageIcon(getResource("img\\AreaComponent\\Grass\\1.gif"))).getImage(),
    		false);
    	}
    }
    Last edited by AndrewM16921; 10-07-2009 at 04:23 PM.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

  5. #5
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    To build upon what masijade mentioned about "getResource":

    To understand where things are to your existing file path:

    Java Code:
    File file = new File(""); //this is currently where you are looking for your grass
    System.out.println("[not going to find it here]: " + file.getAbsolutePath());
          
    System.out.println("[were are getting closer]: " + this.getClass().getResource(""));
    
    System.out.println("[perhaps here (is it not null)?]: " + this.getClass().getResource("img/AreaComponent/Grass/1.gif"));
    System.out.println("[perhaps here (is it not null)?]: " + this.getClass().getResource("../img/AreaComponent/Grass/1.gif"));
          
    //When you find the something that is non-null then:      
    ImageIcon foundIt = new ImageIcon(this.getClass().getResource("img/AreaComponent/Grass/1.gif"));
    My Hobby Project: LegacyClone

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    I'm still immensely confused. For your code example, I kept getting null, even after I messed with it a bit based on the sun tutorial. Maybe if I post two of my classes (AreaComponent and Grass), someone can try to explain how to go about using getResource() properly. I've tried like 6 different implementations, each of which either gives me an error or a nullPointerException.

    Java Code:
    import javax.swing.ImageIcon;
    
    public class Grass extends AreaComponent
    {
    	public Grass()
    	{
    		super(new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), false);
    	}
    }
    Java Code:
    import java.awt.Image;
    
    public class AreaComponent
    {
    	private Image occupiedImage, unoccupiedImage;
    	private boolean occupied, solid;
    	
    	public AreaComponent(Image occupiedImg, Image unoccupiedImg, boolean sol)
    	{
    		occupiedImage = occupiedImg;
    		unoccupiedImage = unoccupiedImg;
    		solid = sol;
    	}
    	
    	/* Run this method if the player enters this space */
    	public void playerEnter(Area area, Player player)
    	{
    		occupied = true;
    	}
    	
    	/* Run this method if the player exits this space */
    	public void playerExit(Area area, Player player)
    	{
    		occupied = false;
    	}
    	
    	/* Returns altered Area when player enters */
    	public Area areaActionPlayerEnter(Area area, Player player)
    	{
    		return area;
    	}
    	
    	/* Returns altered Area when player exits */
    	public Area areaActionPlayerExit(Area area, Player player)
    	{
    		return area;
    	}
    	
    	/* Sets occupied to occupy */
    	public void setOccupied(boolean occupy)
    	{
    		occupied = occupy;
    	}
    	
    	/* Returns the appropriate Image */
    	public Image getImage()
    	{
    		if(!occupied)
    			return unoccupiedImage;
    		else
    			return occupiedImage;
    	}
    	
    	/* Returns true if the AreaComponent is currently occupied, false otherwise */
    	public boolean isOccupied()
    	{
    		return occupied;
    	}
    	
    	/* Returns true if the AreaComponent is solid, false otherwise */
    	public boolean isSolid()
    	{
    		return solid;
    	}
    }

  7. #7
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    What is displayed when you run this code?

    Java Code:
    import javax.swing.ImageIcon;
    
    public class Grass extends AreaComponent
    {
    	public Grass()
    	{
    		//super(new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), false);
                 super(null, null, false);
                 javax.swing.JOptionPane.showMessageDialog(null, "" + this.getClass().getResource(""));
    	}
    }
    My Hobby Project: LegacyClone

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Have you bothered reading that tutorial yet (probably not), there is a section dedicated to using getResource for loading the images. I don't post things for no reason.

  9. #9
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Quote Originally Posted by masijade View Post
    Have you bothered reading that tutorial yet (probably not), there is a section dedicated to using getResource for loading the images. I don't post things for no reason.
    Quote Originally Posted by AndrewM16921 View Post
    I'm still immensely confused. For your code example, I kept getting null, even after I messed with it a bit based on the sun tutorial. Maybe if I post two of my classes (AreaComponent and Grass), someone can try to explain how to go about using getResource() properly. I've tried like 6 different implementations, each of which either gives me an error or a nullPointerException.
    Yes, I did look at the tutorial. I didn't seem to understand it entirely, but I will look again.

    Quote Originally Posted by mrmatt1111 View Post
    What is displayed when you run this code?

    Java Code:
    import javax.swing.ImageIcon;
    
    public class Grass extends AreaComponent
    {
    	public Grass()
    	{
    		//super(new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), new ImageIcon("img\\AreaComponent\\Grass\\1.gif").getImage(), false);
                 super(null, null, false);
                 javax.swing.JOptionPane.showMessageDialog(null, "" + this.getClass().getResource(""));
    	}
    }
    It prints:
    file:/F:/Workspace/DarkBrotherhood/bin/

    [hr]

    Now, I'm thinking something like
    Grass.class.getResource("img\\AreaComponent\\Grass \\1.gif")
    But I'm not sure exactly how to implement this.
    Last edited by AndrewM16921; 10-07-2009 at 11:39 PM.

  10. #10
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    Edit: Nevermind.
    Last edited by mrmatt1111; 10-08-2009 at 01:11 AM.
    My Hobby Project: LegacyClone

  11. #11
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    I actually figured out a way to do it without using getResource. Thanks for the help, anyway.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AndrewM16921 View Post
    I actually figured out a way to do it without using getResource. Thanks for the help, anyway.
    getResource is the proper solution here, so any "solution" that doesn't use it is possibly suspect. Please explain your solution, so we can make sure that you're not shooting yourself in the foot.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    P.S. With getResource you use "/" not "\\", and, now, look at the following situation

    Your jarfile has the following setup

    Java Code:
    package1
      package2
        YourClass.class
        img
          yourimage.gif
    now from YourClass you use getResource as follows:

    getResource("img/yourimage.gif")


    now suppose your jarfile is setup as follows

    Java Code:
    package1
      package2
        YourClass.class
    img
      yourimage.gif
    Now getResource is as follows:

    getResource("/img/yourimage.gif")


    Do you see the difference in those? A leading "/" on the path in getResource means "start from the package root". If a leading "/" is not present it means "start from the current package".

    Do you understand?

  14. #14
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default

    Yes, that makes sense, thank you. I will try that out as well.

    As for my other solution, I simply put the img folder parallel to the jar file rather than inside of it. I would rather have it as a single jar file, so I will look into what you posted when I get a chance. Thanks. =)

Similar Threads

  1. code run with eclipse but not with bat file
    By livnihai in forum Eclipse
    Replies: 15
    Last Post: 09-17-2009, 03:58 PM
  2. jar file eclipse
    By vishnu5 in forum Eclipse
    Replies: 5
    Last Post: 09-08-2009, 08:15 PM
  3. Eclipse Bug - Can't Read From A File Using Eclipse?
    By carlodelmundo in forum New To Java
    Replies: 6
    Last Post: 01-26-2009, 04:25 PM
  4. jsp file in Eclipse
    By nagaprasanna in forum Eclipse
    Replies: 0
    Last Post: 11-18-2008, 08:21 AM
  5. eclipse batch file
    By doug99 in forum Eclipse
    Replies: 6
    Last Post: 04-24-2008, 02:59 PM

Tags for this Thread

Posting Permissions

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