Results 1 to 7 of 7
  1. #1
    satory is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default BufferedImage, ImageIO and Jar files

    Im having a small problem with moving my code over to a Jar file...

    What I have at the moment is the following line of code,

    Java Code:
    bgImg = ImageIO.read(new File("data/Images/bgImg.png"));
    In eclipse when I run the project it runs fine, the image is picked up. I have tried exporting the project to a Jar file but the images wont show when I run the jar. I have tried moving the data folder into the Jar file, and also having the jar file and data folder in the same location, but still no luck...

    Earlier today I changed the code to this,

    Java Code:
    bgImg = ImageIO.read(getClass().getResource("data/Images/bgImg.png"));
    but when I compile the code in eclipse I get a
    Java Code:
    java.lang.IllegalArgumentException: input == null!
    What am I missing?

    please

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    I doubt that you're getting this error when you compile the code but rather when you run the code (there's a big difference here); likely the code compiles just fine.

    One thing to know is that getResource() looks for the resource in a different location than when you got the data via a File. The latter looks for files in a path that's relative to the user.dir, but for the getResourse to work, you'll have to use a path that is relative to the location of your class files.

  3. #3
    satory is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Okay, I dont think Im getting what your saying at the moment...

    How do you mean?
    but for the getResourse to work, you'll have to use a path that is relative to the location of your class files.
    I have tried changing the string in the original examples to have either "/" or "./" before the file path and I have also moved the data folder into the same directory as the source code? but still no luck

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by satory View Post
    How do you mean?
    I have tried changing the string in the original examples to have either "/" or "./" before the file path and I have also moved the data folder into the same directory as the source code? but still no luck
    Where are the image files, such as bgImg.png, in relation to the class files?

    Often I use a resource when my class files and resources are together in a Jar file, and in this situation the resources, while not in the same directory, are in a directory that is not too far from the class files. The resource path has to describe the resource location relative to the directory where the class files are located and loaded from. For instance if the class files in the following directory:

    C:\Users\Pete\Documents\workspace\Pete 2010\src\yr2010\m05\d\

    and my image file bgImg.png is located at:

    C:\Users\Pete\Documents\workspace\Pete 2010\src\yr2010\m05\d\images\

    then my resource path would be "images\bgImg.png".

  5. #5
    satory is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    C:\Users\John\Workspace\project\src\
    C:\Users\John\Workspace\project\data\Images\

    Those would be my two file paths...

    Though I have just moved the data\Images folder into the src folder and still no luck, outta eclipse when I try to run the project... will try and export it to a jar in a min to see if that works

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    What if you try an image resource path that is something like:

    "../data/Images/bgImage.pgn"

    or

    "/../data/Images/bgImage.pgn"

    This appears to me to represent the current path of this resource relative to the class files.

    and I don't think that a Jar will work until you get this to work.

  7. #7
    satory is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    sorry for the delay...

    Got it!!!

    I moved the data/images folder into the src one, but it seems as if the it should of gone into the bin folder :(

Similar Threads

  1. BufferedImage rgb
    By Bill87 in forum New To Java
    Replies: 2
    Last Post: 02-25-2010, 07:31 PM
  2. Using BufferedImage
    By timkd127 in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 10:17 PM
  3. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 06:36 PM
  4. Trouble with ImageIO
    By sari in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 10:42 PM
  5. Help with ImageIO
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:21 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
  •