Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Pranav Thakur
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Pranav Thakur
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try console-->server-->Protocols-->HTTP
√ Accept Context Path In Get Real Path (Select this item)
 
Andy Kay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marcos Maia
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic