What is the difference between JARs and packages?

前端 未结 4 1904
清酒与你
清酒与你 2021-02-03 13:33

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

4条回答
  •  旧巷少年郎
    2021-02-03 14:12

    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."

提交回复
热议问题