wood burning stoves
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes confused between jar and war files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "confused between jar and war files" Watch "confused between jar and war files" New topic

confused between jar and war files

Jim So

Joined: Mar 18, 2009
Posts: 28
I have looked through HeadFirst Servlets + JSP book ed2 and am stuggling to work out the difference between jar files and war files and where they can be located in a web application.
I have a few questions about comparisions that i am struggling with.

Q1. From what i read, they are the same thing - jar and war files. You can rename a war file to a jar file and it will work without having to move any files. And the same with a jar file, you can rename it to a war file and everything will work fine.

Q2. But if they are the same thing, does that mean they are stored in the same places too? Because from what i can see, they are not:

Q3. I have read that you put war files under /webapp folder AND nowhere else. And that ear war file is considered as a separate web application - correct?

Q4. It appears that you put jar files under /webapp/WEB-INF/lib and nowhere else. Is that correct?

I didnt find much info on this area in the HeadFirst Servlets + JSP book ed2.

Thanks in advance for clearing these concerns up for me :-)
Mateus Lucio
Ranch Hand

Joined: Jul 27, 2006
Posts: 60
I haven't studied this part yet, so I will post what I have learned from working with Java and web apps, ok?

Q1 - jar files and war files mean different things, you really can rename a jar into a war and a war into a jar but they probably won't work.
You can say they are the same because the algorithm used to generate this files are the same (creating a jar is kind of zipping a folder) what changes is just the extension, but a war file represents a whole webapp, which means that the files inside a war file obeys the same structure as your application folder inside tomcat for example. A jar file may represent a set of components and classes, like servlet-api.jar, you have got the whole servlet api in it and it's not an application.

Q2 and Q3 - If you put a war file inside tomcat's webapp folder, tomcat will treat it as a webapp and will try to start it (look for a WEB-INF folder, read the web.xml file, create a context, map the servlets, listeners and so on).
You will put jar files into WEB-INF/lib when this jars are used by your web application, like a custom jar with your set of classes, frameworks jar like log4j, hibernate or spring from example.
EARs files are used when you have more than one application (2 war files for exemple) and they need to share resources like jar files, property files or anything else so you don't need to duplicate this resources. EARs are sometimes used to keep things organized too.

Q4 - putting jar into WEB-INF/lib means that your webapp depends on this jars to work and that they are specifically to your webapp. The jars inside WEB-INF/lib will be in your webapp classpath when you deploy it, so when you generate a war file everithing inside the WEB-INF/lib will be packaged together.

hope this info helps and please post again if you didn't understand something.

Studying ...
I agree. Here's the link: http://aspose.com/file-tools
subject: confused between jar and war files
jQuery in Action, 3rd edition