Results 1 to 10 of 10

Thread: File Access

  1. #1
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default File Access

    I wrote a monopoly program using java that accesses a bunch of images by using the specific filepath of them on my computer like JLabel("C:\\user\\documents\\...") etc. but I would like to be able to turn it into a JAR file and send it to some friends. If the JAR file includes all the images how can I make it so that the program will be able to access the images on their computer without specifying the specific filepath? Any help is greatly appreciated!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Use relative paths instead of absolute ones i.e. images/someSprite.png

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

    Default

    Use getClass() method of an abject and then getResource("my path") of the Class object - where "my path" is the path to the image within the jar (relative to the current calling class or absolute - see the API for Class (Java Platform SE 6) to read more)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Also you can use the getResourceAsStream method. Read the API doc as shown by doWhile

  5. #5
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    thank you, but after googling a few things i still don't really understand relative paths and how they work / how to use them..
    could you give a more specific explanation?

    Quote Originally Posted by doWhile View Post
    - where "my path" is the path to the image within the jar (relative to the current calling class or absolute
    doWhile: the problem here is that i don't know the path to the image within the jar because the jar will be on undetermined places of other peoples computers, so how am i suppoed to fill the mypath parameter?

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

    Default

    Quote Originally Posted by littledrummerboy View Post
    doWhile: the problem here is that i don't know the path to the image within the jar because the jar will be on undetermined places of other peoples computers, so how am i suppoed to fill the mypath parameter?
    That's the beauty of the Jar file -- it doesn't matter where the file is in someone else's computer, the path that's important here is internal to the jar and so does not vary. If you created the jar, then you know the relative path.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    don't really understand relative paths
    Write a small test program to read a file an display its contents. Create some test folders and test files with the same name and different contents in each folder. For example: "This file is at Test/Level1" for a file in Test/Level1.
    In your program Change the path used in the program to read the file from different locations. Look at the path used and what is displayed to see how the different paths work

  8. #8
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    That's the beauty of the Jar file -- it doesn't matter where the file is in someone else's computer, the path that's important here is internal to the jar and so does not vary. If you created the jar, then you know the relative path.
    Okay so if the files are just in my src folder then the relative path would be src\image.png ?

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

    Default

    Can you show a picture of your jar file structure?

  10. #10
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    Actually, I just tried it and it worked great, thanks guys

Similar Threads

  1. Replies: 6
    Last Post: 02-10-2011, 10:55 AM
  2. access xml file through factory
    By niteangell21 in forum New To Java
    Replies: 0
    Last Post: 09-30-2010, 07:29 PM
  3. File access
    By frenk_castle in forum New To Java
    Replies: 5
    Last Post: 04-30-2010, 01:34 PM
  4. Unable to access velocity.properties file from jar file
    By mjwoodford in forum New To Java
    Replies: 0
    Last Post: 10-09-2009, 02:46 PM

Posting Permissions

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