• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Filepath Help

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My picture panel gets the image from the same folder that stores my class files in NetBeans but I have a folder called Actors and would like the filepath to be directed there. I have tried to use ./Actors/file.jpg without any succes.

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please describe the file directory hierarchy that you are using. We need this information in order to help you. Also which version of NetBeans are you using? I think NetBeans 4.1 creates a jar file and runs the program from there. However, the user directory will be set differently from the directory that contains the jar. You might want to include the image in the jar file and use Class.getResource() or Class.getResourceAsStream() in order to load the image. Again, we need some more information in order to help you.

Layne
[ January 13, 2006: Message edited by: Layne Lund ]
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using NetBeans 4.1
Filepath: D:\Titres de Film DVD\build\classes\film_titles\Actors
You can see by my code that my serialized file for my actor JavaBeans is in folder SER filepath: D:\Titres de Film DVD\SER
I would like to write the code in the Picture class to be able to get the images in folder Actors D:\Titres de Film DVD\Actors
If I use the "clean and build main project" command in NetBeans, the entire Actors folder is deleted.



 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have only given the path for one directory or file. I was hoping that you would describe the WHOLE directory hierarchy for your project. Since you mentioned that you are using NetBeans 4.1, I think I can make some safe assumptions about this. I understand that your project lives in "D:\Titres de Film DVD". Since you are using NetBeans, there are probably the following directories under this:
  • nbproject - files for NetBeans to manage your project
  • src - this is probably where your source code (.java files) lives
  • test - this is where any JUnit tests typically go
  • dist - this is where NetBeans will put the JAR file and Javadocs if you generate them
  • build - this is where NetBeans will put generated class files

  • Can you fill in any details that I may have missed here? Also please correct some assumptions.

    So it sounds like you put the file.jpg image file under the build\classes\film_titles\Actors directory? This is a big HUGE mistake, especially if this is your master copy of that file. The build directory is TEMPORARY! If you accidentally tell NetBeans to clean your project, the build subdirectory will be deleted! Typically I create a directory called resources to store the master copy of such files.

    Now to answer your question: First you need to get the user directory. To do this you just use the following line of code:

    Any time you use a relative file path (i.e. one that doesn't start with C: or D: or whatever), it will be resolved relative to this user directory. For now, it will probably be simplest to put the Actors directory under this directory. You may want to write some test code that prints this out so you know what directory your program uses.

    You should also learn about different types of paths that are used to locate files. This will include absolute paths and relative paths. Also make sure you understand the meaning of special directory names like . and .. since these come into play when you use relative paths.

    Eventually, you may want to learn how to include these resource files in a JAR file and load them from there. To do so, you will need to look at the Class.getResource() and Class.getResourceAsStream() methods. This will make it easier to distribute your app since you will only need to distribute one JAR file rather than multiple image and class files.

    You should also learn how to modify the build.xml file so that NetBeans can include the resources in the JAR file automatically. NetBeans uses a tool called Ant that uses this build.xml file. Look for Ant documentation to find out more about making these modifications.

    I hope this helps.

    Regards,

    Layne
     
    henri henri
    Ranch Hand
    Posts: 115
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much for your response Layne. You laid out for me some very important concepts that I still need to learn in order to complete my program and have it distributed. All of your assumptions about my file structure were correct. The whole thing is just so overwhelming. The program's imbilical cord attached to NetBeans will eventually have to be severed. You also mentioned some things about how NetBeans works that I have not had time to learn. Thank you for that too.

    I came up with the following line of code in order to get an image in the Actors' folder. You also mentioned using System.getProperty(), I used this earlier on before posting this topic to get the sounds in my program.





    Once again Layne, I really appreciate all the ideas you laid out for me.
    Thank you very much.
     
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Since you brought it up, Layne, I've been having fits with getResourceAsStream(). I added a zip file to the Java build path in Eclipse and can't seem to read the files that are in it. I tried the method on Class and ClassLoader, with and without leading /. Is there something with waving dead chickens over the code that I'm missing?
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Stan,

    Does getResourceAsStream() return a valid InputStream object? Can you read any data at all from it? Did you try wrappnig it in a ZipInputStream to access the information from the zip file? These are the only guesses I have at the moment. If this doesn't spark anything, perhaps you can post some code and a description of where your files are located. It might be helpful if you start your own thread on this.

    Regards,

    Layne
     
    Stan James
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It always returns null, which is documented for not found. When I get back on my work PC I'll post a sample. This code is in a POJO called by a servlet which may tell us something about where the jar should be stored.
     
    You guys haven't done this much, have ya? I suggest you study this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic