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.

60 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. Hi, I couldnt find an option "archived real path" in weblogic 9.2
    Any help pls

    ReplyDelete
  32. Kinetic watches are considered as great investment and energy-saving gadgets, but the solar-powered models can easily compete with them.
    But people are more interested in discussing the three newly-released Maurice Lacroix Pontos
    D. Dress watches are kept small, sleek and delicate to better emphasize the length of your arm and to compliment delicate dress styles
    and fabrics.

    my website http://www.sevilcanasansor.com/index.php?do=/blog/103048/an-analysis-of-effortless-watches-advice/add-comment/

    ReplyDelete
  33. Therefore parents are advised to purchase only bedding
    that is totally suitable for they bed they choose.
    And enjoy your new baby, your whole life has just changed.
    Browse through their listing for the one you are looking for.


    Here is my website; round crib - -

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

    ReplyDelete
  35. brother can give me solution in apache tomcat 7.0

    ReplyDelete
  36. Thank you so much!!! working fine

    ReplyDelete
  37. Rellay Nice Post.It also worked for me after updating Weblogic.xml

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

    ReplyDelete
  39. thanks man, this saved us from a lot of hustle.

    ReplyDelete
  40. I have got a quite silimilar but different issu.. please help me out...

    On opening the browser the reports shows as expected but once we logout (without closing the browser) the above discussed error shows up

    ReplyDelete
  41. The 2nd option worked like a charm. Thank you!

    ReplyDelete
  42. thanks.
    By the way, if you need to get a FileInputStream - there is a way:
    getResourceAsStream()
    for example from servlet:
    request.getSession().getServletContext().getResourceAsStream("images/some_image.png")

    ReplyDelete
  43. Thank you for disclosing 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 and solutions.
    The Lucidtechsystems providing quality weblogic training in hyderabad through clasroom and online along with Build and release training to students world wide.

    ReplyDelete
  44. Great and nice information you have shared.Thanks for sharing.
    weblogic administrator training

    ReplyDelete
  45. Thank you. Its work.

    ReplyDelete

  46. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.I want to share about weblogic administration .

    ReplyDelete
  47. some day in 2024, i came here to say "You saved my life! Thank you!"

    ReplyDelete