Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: File locations?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default File locations?

    Hello,

    I want my Java program (developed with the Eclipse IDE) to parse an XML file.

    Parsing the XML file looks simple enough, but I don't know where to put the XML file. When deployed, the XML should accompany the Java application, but I don't know where to put the XML file while developing.

    I tried putting the file in the bin directory, but I get a file not found error...

    Any advice on where Java expects its files to be at run-time?

    Thanks

    Willsy


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: File locations?

    Java doesn't "expect" anything. You have to tell it where the file is located. That can be an absolute path, or it can be relative. To figure out where Java thinks a relative path is, simply print out the absolute path of a File you create with a relative path.

    Something like:
    System.out.println(new File("test.xml").getAbsolutePath());

    But if you'll eventually be including the File in a jar, you should be using resources instead anyway.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File locations?

    where Java expects its files to be at run-time
    Depends on how the program is executed. If executed from a commandline, the current directory would be here the prorgam would start looking for the files.
    Also it would depend on the path used in the program and what class/method is used to read the files.


    Make a Short, Self Contained, Correct Example to show how you are trying to read the files.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File locations?

    Thanks for the swift reply. After trying your suggestion I can see that Java thinks it's "home" (at least when running my app in the Eclipse environment) is C:\Documents and Settings\Willsy\workspace\F3assembler\test.xml

    That's fine for developing; I now know where to put the file while developing, but I'm worried about deployment time. I would expect the XML file to be placed in the same directory as the .class or jar files?

    --- Update ---

    That was my plan; when I test my program away from the IDE environment I'm just planning to plonk the XML file into the same directory as the java files. That hopefully will do it.

    Later on, I guess I'll create a JAR with an appropriately configured manifest file.

    Thank you for your advice. I'm very new to Java ( < 1 week) but I'm enjoying the language a lot. It's a steep learning curve though, coming from a low-level (assembly language and Forth) background!

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: File locations?

    It depends how you're deploying it. Are you deploying as a jar or a directory of class files? Are you providing the xml file, or is the user? What code you use depends entirely on what you want to do.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File locations?

    Hi Kevin

    The XML file is to be supplied *with* the application.

    Originally, I was intending to simply distribute the class files (zipped) so the XML file would go in the same directory as the class files. However, I've read that JAR files are less hassle for the end user (no worries about ClassPath IIRC).

    I'm open to suggestions and reccomendations.

    Thanks again.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: File locations?

    Jar files are definitely less hassle, especially if your users might not be programmers.

    If you're using a jar, you have a few options. You could still distribute the xml file along with the jar. That gives the user the freedom to modify it if they want, but they could also break things if they move the jar but not the xml file.

    The other option is to put the xml file *inside* the jar. But then it's not called a file anymore, and the File class will no longer work. Stuff inside jar files are called *resources*, and they have their own functions. It's actually much easier than it sounds, and a quick google of "java package resources into jar" gives you several informative links.

    You could even do something fancier and take a hybrid approach: check for the xml file next to the jar, and if it's there, use it. If not, use the default xml file (resource) inside the jar. I'm not sure you want to give your user that much freedom, so it's up to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Willsy (April 10th, 2013)

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File locations?

    If you are supplying the XML file and it won't be changed, it should go in the jar file with the other resources.
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Willsy (April 10th, 2013)

  11. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: File locations?

    Thanks guys. I'll need to google for using Resources. I'll need to look at this now as I'm already halfway through coding the XML decoder (using SAXParser which I really like) and it might influence the code that connects the XML file to the SAXParser, which at the moment is nothing more than:

    saxParser.parse("thefile.xml",handler)

    Hopefully the parse method can accept a Resource as input, rather than a string describing the path and file.

    Thanks

Similar Threads

  1. Run a jar file inside a batch file with Windows 7 Task schduler
    By kingnachi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 09:20 AM
  2. Need help with pie charts locations
    By Wise girl in forum Object Oriented Programming
    Replies: 10
    Last Post: June 13th, 2012, 03:28 PM
  3. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM

Tags for this Thread