Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Filepath Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Filepath Help" Watch "Filepath Help" New topic
Author

Filepath Help

henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
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.

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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 ]

Java API Documentation
The Java Tutorial
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
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

Joined: Dec 06, 2001
Posts: 3061
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

    Joined: Oct 03, 2005
    Posts: 115
    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.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    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?


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    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)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    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.
     
    Consider Paul's rocket mass heater.
     
    subject: Filepath Help
     
    Similar Threads
    Converting from Swing to Applet
    getting problem in creating an image from int array
    placing image on jpanel and then add other components
    JPanel
    how can i refresh a scrollpane