Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Image location on a Runnable JAR file?

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Image location on a Runnable JAR file?

    Hello, i am using eclipse to create my java 2d game... i am trying to export it although the problem is that...when i run the game in Eclipse it works perfectly although on the runnable jar file, it doesnt work because none of the images are working.... the images are in the same folder as the classes (Project>src>Package>) so, i use 'src/package/background.png' when in eclipse, although that is not working when exported..


    so how do i make it so i can use those images in the exported file... ive tried everythign!!, this is a list of what ive tried:


    'src/package/background.png'
    'package/background.png'
    'background.png'
    './background.png'
    './package/background.png'
    './src/package/background.png'
    'project/src/package/background.png'
    './project/src/package/background.png'


    but none of these are working (P.S. i also have tried it with // instead of just / )

    -thanks in advance!!'


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Image location on a Runnable JAR file?

    How are you loading the images? If you do so by using getClass().getResource() the path should be relative or absolute (without the src). If the image is the root path of the jar (src folder in your source directory structure), specify the full path "/background.png"

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Image location on a Runnable JAR file?

    Quote Originally Posted by copeg View Post
    How are you loading the images? If you do so by using getClass().getResource() the path should be relative or absolute (without the src). If the image is the root path of the jar (src folder in your source directory structure), specify the full path "/background.png"
    im not 100% sure what u mean (i tink)...but this is how i call the image:

    Image bg; // at top of class
     
     
    //In my Constructor
    		ImageIcon back = new ImageIcon("src//Game//Background.png");
    		bg = back.getImage();

    and this code works perfectly in eclipse, but not when exported

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Image location on a Runnable JAR file?

    I struggled with this for a second so would've been nice if someone had spelled this out:

    new JLabel(new ImageIcon(getClass().getResource("image.gif"))) works.

  5. The Following User Says Thank You to Jash For This Useful Post:

    JavaPF (March 11th, 2010)

Similar Threads

  1. How to find location of ipaddress using Java
    By tej in forum Java Networking
    Replies: 8
    Last Post: September 8th, 2012, 06:05 AM
  2. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM
  3. Saving an image file (PGM format)
    By Mickey2315 in forum Algorithms & Recursion
    Replies: 3
    Last Post: September 12th, 2009, 01:18 AM
  4. [SOLVED] File Reading from the specified location
    By rajesh.mv in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 13th, 2009, 12:56 AM
  5. setting location of a dialog relative to the parent
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 1st, 2009, 05:42 PM