tags:

views:

57

answers:

3

Hi,

I am using Eclipse to create a Dynamic Web Project. My eclipse is in /home/pc/eclipse

and My project is in /home/pc/workspace/MyWebProj

Now I placed the files in the above Project Directory. When I want to read any File from my code, It always searches the files in /home/pc/eclipse folder instead of /home/pc/workspace/MyWebProj, thus I am hardcoding the file path for time being.

Is there any configuration setting in Eclipse I am missing?

Please Suggest.

Thank You, Tara Singh

+1  A: 

I think what you need to do is read the file from the location of the javax.servlet.ServletContext of the Web Application. If you use getContextPath() you should get the working directory of the Web Application rather than the Java VM.

You could also try loading any files using the getResource and getResourceAsStream methods from the same class.

I was a little rushed when I wrote this so I will provide a little more info.

The best approach is to use the getResourceAsStream method. this allows you to access files within your webapp.

To get a file foo.txt from the root directory of the web application and print it to the response, you could do the following.

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws javax.servlet.ServletException, java.io.IOException
    {
        PrintWriter writer = response.getWriter();

        // foo.txt is at the root directory of the web app
        InputStream in = getServletContext().getResourceAsStream("/foo.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String text;
        while ((text = reader.readLine()) != null) {
            writer.println(text);
        }
    }    
}
Dunderklumpen
Thanks, getContextPath helped me solve this problem. I tried this approach and i am able to read write to files now. Thanks once again.
Tara Singh
A: 

I'm not sure what these files are, or more specifically, what the most appropriate storage location would be, but traditionally the best method of IO in this case is via ClassLoading. You could create a source folder located at src/main/resources that would be included in the archive (jar, war, ear, etc.) assembly (i.e. these files would be included in the jar). You would then load these resources.

If a file is located directly in the package, it will also be included in the artifact assembly, and can be accessed by any class in the package's class loader, or by the default class loader with the package path (packages correspond to folders when an archive is built).

If you are using a maven assembly plugin, these resources files would be included automatically. If you're using something like Ant, you would probably need to include these files in a script step. Otherwise I think if you're simply building in Eclipse there's a wizard for including files.

Also, the likely reason for attempting to read files from the /home/pc/eclipse folder is because a system property like user.dir or user.home is being utitlized:

String filename = System.getProperty("user.dir") + System.getProperty("file.separator") + "file.txt";
hisdrewness
A: 

Hi there,

don't know if it's what you are looking for, but you can set/change the "Working Directory" of each "Running configuration" pretty easily in Eclipse.

  1. Go to the "Run Configurations" Dialog
  2. Select your Project (or the specific running config)
  3. Select the "Arguments" tab
  4. In the lower part of the Window, you can now change the "Working Directory" of the running configuration.
  5. Change it to /home/pc/workspace/MyWebProj and your App should work.

Although this might work, i suggest you should go through a SystemProperty variable. Just get the parent path of your file like this:

File myFile = new File(System.getProperty("com.myapp.myfile.property"));

Now you can reset / move the working dir and filename via the System Properties just by setting this keyword.

Hope it helps a bit ;)

Florian Becke