ServletContext.getRealPath returns null

Another weird behavior from weblogic when webapp is deployed as WAR. ServletContext.getRealPath() returns null when deployed as WAR but it works ok when deployed as exploded. There are two ways we can fix this issue when you still want to deploy as WAR but would like to get over with this issue:
1. Go to server admin console->Domain-> Web applications. Click the checkbox of Archived Real Path Enabled. This should make an entry into domain config.xml as below.
       <web-app-container>
          <show-archived-real-path-enabled>true</show-archived-real-path-enabled>
       </web-app-container>

2. Second option is at webapp level by updating weblogic.xml as below:
      <container-descriptor>
         <show-archived-real-path-enabled>true</show-archived-real-path-enabled>
       </container-descriptor>
The value of <show-archived-real-path-enabled> set in the web app has precedence over the value set at the domain level. The default value of this property is false.

39 comments:

  1. very useful. it worked well

    ReplyDelete
  2. Very much useful tip . Thanks a ton !!!!

    ReplyDelete
  3. I am working on developing a servlet in JDeveloper 11g which needs to "know" the physical path to the Web Content folder. I kept getting "null" when calling ServletContext.getRealPath() and was totally baffled. Following your second option was the "best fit" solution for me and worked like a charm. Thanks so much for posting this!

    ReplyDelete
  4. You saved my life!!! :)

    ReplyDelete
  5. Excellent Help from this blog really thankfull from the bottom of my heart SIR................

    ReplyDelete
  6. Thanks. I started to feel uneasy, because my stuff was not working on the new weblogic.

    ReplyDelete
  7. Thanks a lot! It took me whole day to find solution.

    ReplyDelete
  8. thank you very much

    ReplyDelete
  9. Awesome, thanks a lot. Worked for me.

    ReplyDelete
  10. Used the second option and worked great!! Thank you!!

    ReplyDelete
  11. This is my first time to post here. Thank you so much for your help. The first option worked for me very well but I want to use the second one. May you please tell me where I can find the weblogic.xml? I searched for it in my app but couldn't find it.

    Thanks

    ReplyDelete
  12. weblogic.xml could be found in yourWebApp/WEB_INF directory.

    ReplyDelete
  13. Thank you very much.

    ReplyDelete
  14. Great man thanks for this :)

    ReplyDelete
  15. Thank you for the great help

    ReplyDelete
  16. Great ..!I used the second option and it works very well..
    Thank You so much for posting this!

    ReplyDelete
  17. its only for weblogic 10.x or higer in the version 9.2 not work

    ReplyDelete
  18. Thanks a lot It worked for me.

    ReplyDelete
  19. it's really helpful for us thanks a lot

    ReplyDelete
  20. Hey..Nice tip...But still the the classes under the war/waeb-inf/classes is empty after installing the app..Calling realPath(/WEB-INF/classes/) gives me the path properly, but under the classes nothing is there..Any idea?

    ReplyDelete
  21. You are the man dude!! Second option works nice! Thnx very much!

    ReplyDelete
  22. Thanks ,really it's help me a lot... :)

    ReplyDelete
  23. Looks like this will be useful. Well my problem is similar but for tomcat 7. Will anyone know, how to get rid of getRealpath("/") returning null.

    ReplyDelete
    Replies
    1. you can see your class in a jar file named _wl_cls_gen.jar. this is created by server. you can see this jar in jar folder in your web-inf.

      Delete
  24. Thanks a lot. It worked for me as well with the second option

    ReplyDelete
  25. Hi i tried making the changes in weblogic.xml but true does not show up at all. :( Any suggestions ??

    ReplyDelete
    Replies
    1. *show-archived-real-path doesnt show up i weblgic.xml

      Delete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Awesome!!!! it worked, however my js were not getting loaded and I had to add mime mapping in Web.xml for my js. Well..!! allz well that ends well...
    thanks Ananth..

    ReplyDelete
  28. I am not sure why is it not working for me, can some please help me, when i updated option1 in config.xml my weblogic console is shutting down and to try option2 i am not finding weblogic.xml in application directory, Please please some one help me

    ReplyDelete
  29. This comment has been removed by the author.

    ReplyDelete
  30. Thanks Ananth, the second solution did not work , but the first one did , many thanks ...

    ReplyDelete
  31. 08Ct, the VG cutting technique would ensure crafts concentration.
    Fresh water cultured pearls turn up in a
    wider range of colors and shapes than their saltwater counterparts.

    There is no need to make it fancy, just make it delicious and abundant.


    Here is my website; http://www.nataledonna.com/bessboyle

    ReplyDelete