-2

Location is : C:\Users\md.kamruzzaman\nazdaq\FileWriteToResouces\src\main\resources\xml.

HttpSession session = request.getSession();
ServletContext sc = session.getServletContext();
String x = sc.getRealPath("/" + "WEB-INF\\classes\\xml");

this code produces: C:\Apps\Tomcat\8u21\webapps\FileWriteToResouces\WEB-INF\classes\xml

how can I get this location in web app "C:\Users\md.kamruzzaman\nazdaq\FileWriteToResouces\src\main\resources\xml. "

Rachit Ahuja
  • 371
  • 2
  • 15
  • 1
    It sounds like a bad idea. It would be better if your webapp had a parameter which indicates where you can write such data, for example, in a separate folder. Writing in `WEB-INF\classes\` (which is supposed to be managed by the servlet container) seems dangerous and anti-pattern. – Arnaud Denoyelle Jul 23 '15 at 09:28
  • It is a bad idea. It is a bad idea because it makes your program dependent on behavior that is not defined in the servlet spec, and there are almost certainly Java EE containers that will not allow you access to the underlying file system. – Mikkel Løkke Jul 23 '15 at 09:43
  • Thanks... actually i am new in web apps... but is it possible? – Md Kamruzzaman Jul 24 '15 at 17:27

1 Answers1

0

I'm going to pre-face this answer with a "You really, really shouldn't".

The reasons for this are:

  1. The path may not even exist on the target system
  2. You may not have permission to write files to that folder
  3. Your application may be deployed to a Clustered application server behind a loadbalancer, and the Filesystems might be local, creating bugs that are strange and difficult to debug.

If you still really really want an answer, this question has been answered here: How to get a path to a resource in a Java JAR file

Community
  • 1
  • 1
Mikkel Løkke
  • 3,710
  • 23
  • 37