What is the difference between JARs and packages?

别等时光非礼了梦想. 提交于 2019-12-03 10:03:37

问题


Is there any difference between a JAR file and a package?


回答1:


A package is a way to logically organize your classes. For example, you can declare package com.foo; at the top of each source file that are related enough to reside in the com.foo package together. The Java compiler and runtime will also expect you to place such files in the path com/foo/, where the root of this path is a directory or JAR in your classpath.

A JAR file lets you physically organize your classes. You can take any Java files (and their parent directories, respecting the directory structure discussed above) and store them in a JAR file. A JAR file may contain files belonging to multiple packages, and multiple JAR files may contain files that belong to the same package. So, a JAR file is largely a way to store multiple class files in a single physical file.

There are some other special characteristics of JAR files. For example, you can specify a Main-Class value in the JAR manifest to designate which class is the entry point for an application, and you can seal packages in a JAR file, "which means that all classes defined in that package must be archived in the same JAR file."




回答2:


A package is a mechanism in Java for organizing classes into namespaces. A jar is a Java ARchive, a file that aggregates multiple Java classes into one.




回答3:


In brief, a JAR file is a physical file, structured much like a zip file, that contains files used by your program at execution time. A jar will typically include .class files, the executable versions of your classes, and other resources like icons, pictures, language-specific properties files, etc. The Java Tutorial describes jars, and how to create/manage them. (http://java.sun.com/docs/books/tutorial/jar/index.html)

Packages are defined like this in the Java Tutorial trail on interfaces and packages: "A collection of related classes and interfaces providing access protection and namespace management." (http://java.sun.com/docs/books/tutor...va/interpack/). While accurate, I don't find that this definition, by itself, really gives you a good sense of what a package is. For more information, go to the cited page and read within that section of the tutorial.




回答4:


In classpath jar and package (directory) is same structures. Just jar more usefull for moving between computers than directory.



来源:https://stackoverflow.com/questions/5138383/what-is-the-difference-between-jars-and-packages

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