Results 1 to 6 of 6
  1. #1
    dko
    dko is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default moving files (text/immages) into jar (Solved)

    I currently have a couple folders that have some files, which my program uses. Obviously they don't compile into the jar file NetBeans creates, and just adding them to the jar file doesn't let my program know they are there.
    So I'm wondering how do I best have it so my files are compiled into the jar and that my code will know to look for them there? Hopefully without rewriting a lot of file input/output code.

    Thanks in advance.
    Last edited by dko; 05-27-2011 at 01:56 AM.

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

    Default

    Object defines a method getClass(), and Class defines a method getResource() (or getResourceAsStream() ). So, place the files into the package structure, then from the code call getClass().getResource("path to file") on an instantiated Object where the path to file defines either a relative path to package which defines said object or is absolute (begins with a unix style path delim "/"). See the API for the above methods for a much better explanation than I can provide

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by dko View Post
    I currently have a couple folders that have some files, which my program uses. Obviously they don't compile into the jar file NetBeans creates, and just adding them to the jar file doesn't let my program know they are there.
    Netbeans will add them to the jar file if you tell it to. Either have the folder containing the files in the src folder, or have them in a separate folder and tell Netbeans that that is a source folder.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    without rewriting a lot of file input/output code.
    You will need to read the files as described by doWhile in post #2. The files in the jar are resources not files.

  5. #5
    dko
    dko is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    Ok thanks. I guess ill just have it so it looks for the files as resources first and if it doesn't find them go for the non resource ones.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

Similar Threads

  1. Moving files in Java
    By techbossmb in forum Advanced Java
    Replies: 1
    Last Post: 10-09-2009, 01:55 PM
  2. Help with moving jpeg files
    By Umi in forum New To Java
    Replies: 11
    Last Post: 09-30-2009, 01:11 AM
  3. working with files (text files)
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 11:38 AM
  4. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  5. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04: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
  •