Results 1 to 13 of 13

Thread: Files from .jar

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

    Default Files from .jar

    I've tried looking up how to do this, but I've found a ton of very different methods, so I'm just wondering how to do this, and preferably the (or one of the) best ways to do it.

    I have some images and audio in some directories within my .jar file. My code works fine when run from eclipse, however will not load the files when run from the .jar. Additionally, when I try this:

    dbg.drawString(this.getClass().getResource("banner .jpg").toString(), 5, 60);

    (for debug purposes)

    It works from eclipse, but not in the .jar.

    Here's my relevant code:

    Java Code:
    import ...
    
    ...
    public class Window extends JFrame
    {
    	...
    	Image banner = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("banner.jpg"));
    	...
    	public class GamePanel extends JPanel implements Runnable
    	{
    		...
    		private Graphics dbg;
    		...
    		private void render()
    		{
    			...
    			else if(state == State.MENU)
    			{
    				//this code works in eclipe, not in a .jar
    				dbg.drawImage(banner, PWIDTH/2 - banner.getWidth(this)/2, 60, this);
    				...
    				dbg.drawString(this.getClass().getResource("banner.jpg").toString(), 5, 60);
    			}
    		}
    	...
    	}
    }
    Last edited by AndrewM16921; 04-05-2011 at 04:16 AM.

  2. #2
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    When I did something similar I used:
    Java Code:
    ClassLoader cldr = this.getClass().getClassLoader();
            ImageIcon icon = null;
            try {
                java.net.URL imageURL = cldr.getResource("filtra/resources/images/" + iconString + ".jpg");
                icon = new ImageIcon(imageURL);
            } catch (Exception e) {
            }
    Check if that works for you. "filtra" is the name of my project folder.

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

    Default

    I tried that, works in eclipse, but now the jframe won't even load from the jar... >_> sigh

  4. #4
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Could you post the whole .java file so we could take a look. Maybe there is something wrong somewhere else in the code too...

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

    Default

    -removed-
    (see below reply)
    Last edited by AndrewM16921; 04-05-2011 at 06:22 AM.

  6. #6
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    I meant upload it ^^ and post the link :P
    Upload all the java files so I can compile. But I have to go now I may be able to answer in 5-6 hours...
    Last edited by santeron; 04-05-2011 at 06:21 AM. Reason: added ps

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

    Default

    Last edited by AndrewM16921; 04-05-2011 at 06:24 AM.

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

    Default

    I'll upload em shortly

    but I'll PM you the link cause I don't really wanna post the whole thing for the world to see, at least not til I'm done with it lol
    Last edited by AndrewM16921; 04-05-2011 at 06:28 AM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    I'll PM you the link cause I don't really wanna post the whole thing for the world to see
    Abuse reported for trying to take the discussion off-forum.

    db

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

    Default

    Look in the jar file to see where the image file has been put.
    I would lay good odds that there's something up with your setup in Eclipse...

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

    Default

    Quote Originally Posted by Tolls View Post
    Look in the jar file to see where the image file has been put.
    I would lay good odds that there's something up with your setup in Eclipse...
    Turns out it didn't put the image in the .jar x_x I could've sworn I've checked it before and it was there... hmmm. I'll have to mess around with it to see if I can manually fix it that way. :)

    Quote Originally Posted by Darryl.Burke View Post
    Abuse reported for trying to take the discussion off-forum.

    db
    Wow, get a life kid. Sorry for not wanting to share my entire project with the public? Lol...

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

    Default

    Thanks Tolls. I fixed it by adding the images manually into the .jar. :)

  13. #13
    santeron is offline Member
    Join Date
    Apr 2011
    Location
    Athens, Greece
    Posts
    52
    Rep Power
    0

    Default

    Quote Originally Posted by AndrewM16921 View Post
    Wow, get a life kid. Sorry for not wanting to share my entire project with the public? Lol...
    haha I guess you used private classes just for me :P

    db really unnecessary action imo...

Similar Threads

  1. Replies: 8
    Last Post: 07-30-2010, 06:47 PM
  2. import from dsv files and export to csv files
    By 10x2 in forum New To Java
    Replies: 12
    Last Post: 04-30-2010, 12:56 PM
  3. working with files (text files)
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 12:38 PM
  4. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 04:20 PM
  5. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 05:43 AM

Posting Permissions

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