Java – what is the difference between jars and packages?

What is the difference between jar files and packages?

Solution

A package is a method of logically organizing classes For example, you can declare the package com foo; At the top of each source file, they are relevant enough to reside together on COM Foo bag The java compiler and runtime also want you to put these files in the path COM / foo /, where the root directory of this path is a directory in the classpath or jar

Jar files allow you to physically organize your classes You can take any Java file (and its parent directory, respecting the directory structure discussed above) and store them in a jar file Jar files may contain files belonging to multiple packages, and multiple jar files may contain files belonging to the same package Therefore, jar files are largely a way to store multiple class files in a single physical file

Jar files also have some other special features For example, you can specify a main class value in the jar list to specify which class is the entry point of the application, and you can use the seal package in the jar file, "which means that all classes defined in the package must be archived in the same jar file“

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>