一个 .jar 文件是否需要在计算机上安装所有类才能运行?

huangapple go评论65阅读模式
英文:

Does a .jar file need all of the classes installed on a machine to run?

问题

一个 .jar 文件在运行时是否需要在计算机上安装所有的类?例如:如果我将一个程序打包成 .jar 文件进行分发,是否还需要在用户的计算机上安装我所有的 .class 文件?如果是这样的话,是否有将 Java 程序打包成单个文件(比如可执行文件)的方法?

英文:

Does a .jar file need all of the classes installed on a machine to run? For example: if I ever distribute a program as a .jar file, will I also need to install all my .class files on the user's machine? If so, is there a way to distribute a java program as one file (such as an executable)?

答案1

得分: 1

这取决于您如何构建这个 JAR 文件。您的 JAR 文件中的类可以依赖于该 JAR 中的其他类,以及系统中其他已安装的 JAR 文件中的类。对于后一种情况,如果您将该 JAR 文件分发给其他用户,它们可能无法正常工作。

您需要确保您构建和分发的 JAR 文件包含了您使用的所有依赖项。我听说 Maven 在这方面可能会有帮助。

英文:

It depends on how you built the jar. Classes in your jar can depend on other classes in your jar, and/or on classes in other jars installed on your system. In the later case, they probably won't work if you distribute the jar to other users.

You need to ensure that the jar you build and distribute contains all dependencies you are using. I've heard that Maven can help here.

答案2

得分: 0

类被编译到.jar文件中,因此您不必在每台机器上安装它们。是的,有一种方法可以分发可执行的.jar文件。

英文:

The classes are compiled into the .jar file, so you do not have to install them on every machine. And yes there is a way to distribute an executable .jar file.

答案3

得分: 0

实际上,.jar 文件是可执行的。您可以使用 java -jar jarFile 命令来执行它。

您的 jar 文件是否包含了运行所需的所有类取决于您构建它的方式。
您可以将所有依赖项打包到您的 jar 文件中,这被称为 "fat jar",或者您可以只声明您的依赖关系,并且使用像 Maven 或 Gradle 这样的构建工具仅将自己的类打包到 jar 文件中。在后一种方式中,用户可以使用相同的构建工具下载依赖项。通常情况下,后一种方式更好。

英文:

In fact a .jar file is an excutable. You can execute it with java -jar jarFile.

Whether you jar contains all classes to run the jar depends on how you built it.
You can pack all denpendencies in you jar which is called a fat jar, or you can just declare your dependencies and just pack you own class in the jar with build tools like maven or gradle. In the later way the user can download the dependency with the same build tool. Usually the later way is better.

huangapple
  • 本文由 发表于 2020年8月31日 07:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63663004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定