Results 1 to 2 of 2
  1. #1
    Juuno is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default working directory in web application

    I have my Java project, say in C:\User\Workspace\myApplication
    And there’s an xml file in that folder: C:\User\Workspace\myApplication\myXML.xml

    And in my java program, I will take that xml file to retrieve information. So, I need to call it from my program. And now I am loading that file like:
    File xmlfile = new File(“C:\\User\\Workspace\\myApplication\\myXML.xm l”)

    And I know that it’s not a good way to load a file using directly the absolute path. So, I try to call it from my program dynamically. Here, my program is web application. And I use Tomcat.

    First, I tried this way:

    File directory = new File(“.”);
    String loc = directory.getCanonicalPath();
    File xmlfile = new File(loc + “myXML.xml”);

    And, second I tried using: System.getProperty(“user.dir”);

    Both first and second way give me the same result. When I tried to test those methods as Java application, they both gave me the path : C: \User\Workspace\myApplication

    But when I tried to put it in my program which accepts input from JSP and run on server, then the result path is: C:\Eclipse. So, there’s an error that the system cannot find the file.

    Why it cannot find the actual path?
    And how can I do it to load that xml file into my Java program.

    Any reply would be greatly appreciated.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Rep Power


    If you want to "get" a file on the client, forget this entirely and Google "Java File Upload". If you want a file on the server, place it somewhere in the web application directory and start with getResource/getResourceAsStream/getContext/getContextPath/getRealPath calls of ServletContext.

    Java EE 5

Similar Threads

  1. contentType="application/" Not Working
    By JordanParfitt in forum Advanced Java
    Replies: 1
    Last Post: 02-06-2009, 04:16 PM
  2. JNLP - Swing application compiled in jdk1.5 not working in jdk1.6
    By mahendra.athneria in forum AWT / Swing
    Replies: 4
    Last Post: 01-20-2009, 09:27 PM
  3. Static function to get current working directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-23-2008, 03:13 AM
  4. Replies: 8
    Last Post: 05-28-2008, 08:00 AM
  5. How to get the current working directory in EJB?
    By sathish_2111 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-19-2007, 05:24 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