Glassfish: Setting the context-root of a WAR inside an EAR

I’ve been bashing my head against THIS one for weeks, on and off.  Each time I tried to figure out how to map my app to the name of my choice (‘/’) nothing I did seemed to work!

Finally today I learned about yet another XML file you have to edit in Java EE – application.xml.  Yep, the Java EE development life is filled with wonderful and magical XML files.

Application.xml is a lot like web.xml, but it applies to the EAR.  What isn’t obvious, and which nobody warned me about, is that the context-root for an app is set in application.xml, and it doesn’t matter what you put into sun-web.xml, web.xml, or anything else to try and rename the context-root of your war.  application.xml will override all of that.

So, I’m happy to have found it, but sad that it took so long.  Hopefully others having the same problem – “context-root in sun-web.xml ignored” or “glassfish ignores my context-root” for those searchers out there – can find this post and be enlightened by it!


26 Responses to Glassfish: Setting the context-root of a WAR inside an EAR

  1. Alexis MP says:

    Thanks for sharing this.

  2. cw says:

    Thanks for sharing this information. Like you, I also spent quite some time trying to achieve this supposedly easy task.

  3. Dj Rabbit JAvA says:


    This was just what I needed. I also did not quite figure out what happend.

    Thank your for sharing this!

  4. Stephan says:

    Thanks, had are hard time looking into this.

  5. xlinuks says:

    The GlassFish server has a server GUI manager available at localhost port 4848 through the browser.
    One can log in (default username/password are “admin” and “adminadmin” respectively), go to Applications->WebApplications, click on the name of your one and you’ll see to the right the Context Root option which you can change and click “Save” and you’re done.

  6. dobes says:

    Thanks xlinuks – that’ll help for a a WAR file (a Web Application), but this tip is for people who’ve created an EAR file (an Enterprise Application). In the version of glassfish I have, I can’t change the context root of a WAR file that’s bundled inside an EAR file using the admin interface, although I can, as you’ve shown, change the context root of a WAR file.

    FWIW, anyone who didn’t know already, you CAN change the context root of a WAR file by editing it’s web.xml or sun-web.xml, as long as it’s not being deployed as part of an EAR file.

  7. FWIW, if you’re using NetBeans and create the EAR in there, you can then right-click on the EAR icon in the project view (triangle icon in NB), select New…, and choose “Standard Deployment Descriptor…”.

    This will cause NetBeans to create and populate the application.xml file, including referencing the EAR’s source war files and putting in your already-defined context roots.

  8. dobes says:

    @Greg – thanks for the tip. I’m not a NetBeans user but they may find that useful!

  9. novagirl says:

    Thanks for the NetBeans tip!

  10. Thanx!
    I was also baffled there for a while.

  11. guest says:

    Dobes, thanks a lot for this information. I am new to glassfish and I am currently having sleepless nights over trying to render a jsf page, packaged in a war file, which in turn is in a ear file. The ear is successfully deployed in glassfish hotdeploy directory but when I try to render it by typing:


    In the server.log, I see:

    …File “$com.sun.aas.instanceRoot}/applications/glassfish/domains/domain1/docroot//pages/new.jspx” not found|#]

    Do you know why it is looking in docroot? I have google and found a few questions raise on the same issue but no answer (I wonder why). Any hint would be enormous help. And thanks in


  12. dobes says:

    Hello “Guest”, if that’s REALLY your name!

    I haven’t run into this problem myself, but it seems suspcious that you have two slashes before pages. What is the name of your war file? Have you tried accessing it using that name?

    Good luck …

  13. guest says:

    Dobes, thanks for your response.

    >>you have two slashes before
    I had the context-root name (or place holder inside < and >) between the two slashes, which the page did not render.

    >>What is the name of your war file?

    The name of the war file is ‘foo-web.war’ and the ear is ‘foo.ear’. But I am deploying the foo.ear containing the web.war. And I have my context-root set to ‘/foo-web’–formerly in the ‘sun-web.xml’ and ‘sun-application.xml’ and now (albeit unnecessary) in the ‘application.xml’ .

    >>Have you tried accessing it using that name?
    Yes I have tried foo, foo-web, i.e., the name of the war with no success.

    Glassfish deployment instruction says:
    1 deploy [ear, war]to autodeploy dir. and then do:
    2. http://localhost:8080/foo-web/pages/new.jspx.

    Is there something I am missing between steps 1 and 2?

    Thanks again for you help.

  14. dobes says:

    Those instructions sound fine to me – maybe you can check in the logs or the admin web interface to see if it has been deployed or not, and if there were errors during deployment. It can also help to browse around in the domains/domain1/applications/j2ee-apps and see what the folder structure there is. I don’t think I have time to really dig deep into this one … it’s probably something very simple once you figure it out.
    Have you tried just deploying something simple, like a WAR file, or something someone else has already made?

  15. guest says:

    >> I don’t think I have time to really dig deep into this one

    Thanks for your response, but as I said in my first post, I am able to deploy foo.ear sucessfully—server.log, verifier-result’s foonnnnnnn.txt, and console, didn’t record any failures, warnings or errors.

    Once again, thank you.

  16. Robert says:

    Thanks for the post, This simple problems are the ones that drive someone crazy.

  17. Bartek says:

    Thanks for help 🙂

  18. psm says:

    I just started with ee application. I by mistake deleted a web project that i was experimenting with. now the glass fish gives the following error.

    The context root [EJBTrials-war] in application [EJBTrials-war] is already in use by another application on this instance [server].
    Can i change the context root element and solve my problem? If so how?
    Thanks in advance.

  19. Hi psm,

    Sadly, I don’t know the answer to your question – try the glassfish mailing lists or forums!

  20. Remy Monsen says:

    Thanks for this. I was pulling my hair out looking for the solution to this one.

  21. Robert says:

    thanx for this small tutorial, works perfectly 😉

  22. AJ da DJ says:

    Thanks! for throwing light on this ‘n sharing 🙂

  23. Dexter says:

    Hi everyone!
    I have just deployed a .ear file and realised that the sun-web.xml does not exist. Can I just create this file and put into the WEB-INF folder? I also have tried to change all the application.xml but unable to fix the context root.

    Thank you! 🙂

  24. Paul says:

    Greg, thanks for the NetBeans tip!
    That was much help!

  25. Andi says:

    Thx for sharing, this setting would have took me like forever to find.
    For the eclipse users: right click on your ear project, select “java ee tools”, “generate deployment descriptor stub”.

  26. Steve Ferguson says:

    Thank you. This saved me a ton of time today.

%d bloggers like this: