tags:

views:

23

answers:

1

I develop Java EEapplication and I have to create some files in the src folder in the my application. But I dont where or how to specify the path. I want my application to be portable. It means just to say TestProject/src/.... and not C:/bla/bla/TestProject/src/...

do we specify this in some app file like web.inf, meta-inf or its just some trick.

Thanks!

A: 

If its is a web application, you can find out the location of the temporary directory then create your files there. Eg

public void doGet(HttpServletRequest req, HttpServletResponse res)
   throws Exception {

   File f = (File)getServletContext().getAttribute("javax.servlet.context.tempdir");
   if (f != null) {
     // Do something with f
   } else {
     // We don't have a temporary directory. Fall back to a preconfigured directory 
     // from web.xml <init-param>
   }
}

See this.

Chuk Lee
and what if I have JSF application? Can I get the real path from a simple class? OK, there possibility to get faces context like: FacesContext.getCurrentInstance(); but about the real path I have no idea.
Milan
You do something like this FacesContext fc = FacesContext.getCurrentInstance(); ServletContext sc = (ServletContext)fc.getExternalContext().getContext();
Chuk Lee
ok, it works. But Im still little confused.Actualy I do this. In runtime I generate classes, I compile them and then I use them. Where do you prefer to create them? in "src" folder and compile to build/classes? And after how to get the absolute path to them?
Milan
also with you code I get C:\......\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\TestPorject\
Milan