GeeCON Prague 2014*
The moose likes BEA/Weblogic and the fly likes getServletContext().getRealPath( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "getServletContext().getRealPath("WEB-INF/classes") returns null from EAR file in WebLogic 9.2" Watch "getServletContext().getRealPath("WEB-INF/classes") returns null from EAR file in WebLogic 9.2" New topic
Author

getServletContext().getRealPath("WEB-INF/classes") returns null from EAR file in WebLogic 9.2

Pranav Thakur
Greenhorn

Joined: Apr 07, 2009
Posts: 2
I want to get the real path of a file in WebLogic 9.2. When I do this via an enterprise application it gives the real path. Whereas, when I do it via an EAR file then it returns null. I also found out that this is a security feature introduced by WebLogic 9.2. I also tried using a configuration tag in weblogic-application.xml but it didn't work. Please let me know if there is a work around for this.


Thanks,
-Pranav
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

Welcome to the JavaRanch.
Why do you need to path to the classes directory? If you are trying to read some file, use Class.getResource() or Class.getResourceAsStream().


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Pranav Thakur
Greenhorn

Joined: Apr 07, 2009
Posts: 2
Hi there,

That was just a sample code. There is a word doc for which I need the exact path. I tried it out with this and it worked:

getServletContext().getResource("/resources/CONFERENCE_EMAIL_ATTACHMENT_FILE).getPath().substring(1)

Do let me know if there's another work around for this.

Thanks anyways
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Hi,

this is stated in specification. getRealpath() method is supposed to return null from packaged applications.



This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).


Javadocs also say that:

http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/index.html

take a look at ServletContext.getRealPath() in docs above.


You should consider mapping a directory if you need access to static resources. If you're loading some properties or configuration files(xml) you should NOT use getRealPath to find it.

[]s
Dongyun Tian
Greenhorn

Joined: Oct 29, 2008
Posts: 10
try console-->server-->Protocols-->HTTP
√ Accept Context Path In Get Real Path (Select this item)


Anyone can carry his burder,however hard,until nightfall.<br />Anyone can do his work,however hard,for one day.
Andy Kay
Greenhorn

Joined: Oct 26, 2009
Posts: 29
This gave me nightmares. I don't know why weblogic behaves differently when deployed as WAR instead of exploded. There are 2 ways to fix this - domain level and webapp level. please visit this blog entry for the same.

http://ananthkannan.blogspot.com/2009/12/servletcontextgetrealpath-returns-null.html

Hope this helps.
Andy


http://ananthkannan.blogspot.com
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Andy Kay wrote:This gave me nightmares. I don't know why weblogic sometimes behaves differently when deployed as WAR instead of exploded.



Weblogic behaves like this because THIS IS EXACTLY what is defined in the J2EE specification. This is not a problem and is an expected and correct behavior as stated in specs. This is also mentioned in javadocs.

http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/index.html

[]s
 
GeeCON Prague 2014
 
subject: getServletContext().getRealPath("WEB-INF/classes") returns null from EAR file in WebLogic 9.2