Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By doWhile

Thread: How to read Java image from relative path?

  1. #1
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default How to read Java image from relative path?

    Hey

    I have this code:

    Java Code:
    getImage = new GetImage("/src/part1/image.png");
    But I get an FileNotFoundException. I wonder why? What is the correct way to declare a relative path?

    part1 is a package in my src file. I'm working with netbeans and image.png is in the package folder with all other classes.

    thank you!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default Re: How to read Java image from relative path?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to read Java image from relative path?

    "/src/part1/image.png" is not a relative path, it is absolute - absolute depending upon how you are trying to read the image, which you have not specified...one of the reasons we often ask for an SSCCE - so threads don't turn into a guessing game.
    DarrylBurke likes this.

  4. #4
    Join Date
    Dec 2011
    Location
    Belgium
    Posts
    25
    Rep Power
    0

    Default Re: How to read Java image from relative path?

    What's an SSCCE? BTW I thought that if an image is in the same folder as your classes are, you could just write something like .../image.png to get the file. Guess it's not that easy.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: How to read Java image from relative path?

    What's an SSCCE?
    Let me google that for you

    I thought that if an image is in the same folder as your classes are, you could just write something like .../image.png to get the file
    Again, it depends upon how you are loading it (which again, no info provided). Java uses Unix/Linux type Paths to files, but the root of that path may be relative to the package, relative to the jar, or relative to the system.

  6. #6
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Re: How to read Java image from relative path?

    Quote Originally Posted by The original stinger View Post
    Hey

    I have this code:

    Java Code:
    getImage = new GetImage("/src/part1/image.png");
    But I get an FileNotFoundException. I wonder why? What is the correct way to declare a relative path?

    part1 is a package in my src file. I'm working with netbeans and image.png is in the package folder with all other classes.

    thank you!
    I would use something with portability. The System.getProperty( "user.dir" ) returns the path that the .class resides in.
    Last edited by stchman; 05-02-2012 at 07:20 AM.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. specify relative path to file in web.xml
    By trevMD in forum Web Frameworks
    Replies: 2
    Last Post: 04-28-2012, 01:23 AM
  2. FileInputStream with relative path in a JAR
    By Bertieboy7 in forum New To Java
    Replies: 3
    Last Post: 03-31-2011, 04:10 AM
  3. How to use a relative path for a file?
    By kipade in forum SWT / JFace
    Replies: 0
    Last Post: 02-28-2011, 06:37 AM
  4. how to set relative path in jsp?
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-02-2009, 08:20 AM
  5. Obtaining relative path outside of executable JAR
    By tjsaker in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2008, 06:48 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
  •