JAR vs WAR file specification

笑着哭i 提交于 2019-12-18 09:04:31

问题


I noticed that WAR files are supposed to have classes/ and lib/ directories for containing their classfile root and dependencies, respectively.

I also noticed that it is not common practice for JARs to contain such a lib/ directory and to contain their own dependencies.

So now I'm wondering why JARs shouldn't/usually-don't contain their own dependencies, but WAR files are expected to. Unless I'm missing something, both require their dependencies to be on the classpath at runtime (JARs won't run if they're missing dependencies, just like WARs won't run). So to me, all the arguments for putting dependencies in a WAR file also apply to a JAR.

What am I not "getting" here?!?


回答1:


From a conceptual point of view:
A jar typically represents a single library which can be used and may or may not have dependencies.
But the idea is that a specific functionality can be provided as a library in a jar.

A war is an application by itself and as such it should include all the dependencies




回答2:


WAR files (mainly WEB applications) are uncompressed, and all JAR files inside the lib folder will be in the classpath of your web application while running.

JAR files might also contain jar files, but will not be found and loaded by the default class loader.

There are special class loaders that do load jar files inside jar files, but not standard.




回答3:


Because unless you use a custom classloader that can load classes from nested jars (like JarClassLoader), you can't load classes from nested jars. Servlet and app containers will add jars contained inside war/ear files to their application's classpath.




回答4:


They are containers with different purposes.

  • Jar is intended for Java class files and resources and can be a library or a directly runnable application.

  • War is a container to package complete Web applications including all dependencies (such as Jar files and other web resources).

The question boils down to, why can't a jar include other jars. This I would take as a design decision taken to allow maximum flexibility in interchanging dependencies.



来源:https://stackoverflow.com/questions/9723888/jar-vs-war-file-specification

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!