java .jar 和 .dll 文件的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4411028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:14:48  来源:igfitidea点击:

Difference between .jar and .dll file

c#java

提问by Tarik

I am learning Java these days and I've spent a lot of time with .NET so when I want to export or import libraries, they are usually be in .dll format which is called assembly in .Net environment and they are compiled to IL and they can have resources like images, xml, audio and son on, any ways.

这些天我正在学习 Java,我在 .NET 上花了很多时间,所以当我想导出或导入库时,它们通常是 .dll 格式,在 .Net 环境中称为程序集,它们被编译为 IL他们可以以任何方式拥有图像、xml、音频等资源。

I am wondering same process in Java as well. I've read documents but they actually confused me little bit and to clarify things out I need your help guys.

我也想知道 Java 中的相同过程。我已经阅读了文档,但它们实际上让我有点困惑,为了澄清问题,我需要你们的帮助。

Questions:

问题:

  1. .NET Assembly is same thing as java .jar?
  2. .dll contains compiled IL code and .jar contains complied .class/byte code files?
  3. They say resources, what kind of resources we are talking about here? Images, .txt files, etc. or all of them possible?

  4. I've examined AWS (Amazon Web Service API for java) and I saw three .jar file and they are

    1. aws-java-sdk-1.1.1.jar
    2. aws-java-sdk-1.1.1-javadoc.jar
    3. aws-java-sdk-1.1.1-sources.jar
  1. .NET 程序集与 java .jar 是一回事吗?
  2. .dll 包含编译的 IL 代码,.jar 包含编译的 .class/字节码文件?
  3. 他们说资源,我们在这里谈论什么样的资源?图像、.txt 文件等或所有可能的文件?

  4. 我检查了 AWS(用于 Java 的 Amazon Web Service API),我看到了三个 .jar 文件,它们是

    1. aws-java-sdk-1.1.1.jar
    2. aws-java-sdk-1.1.1-javadoc.jar
    3. aws-java-sdk-1.1.1-sources.jar

and they contain .class files - java documentation in html format and .java files which are still not compiled. So then I've realized .jar doesn't just include compiled byte codes (.class) and also other things.

它们包含 .class 文件 - html 格式的 java 文档和尚未编译的 .java 文件。然后我意识到 .jar 不仅包括编译的字节码 (.class) 和其他东西。

  1. When I want to import java libraries, will I always need to import .jar files?
  2. When I want to export my own java libraries, should I need to export in .jar file.
  1. 当我想导入 java 库时,是否总是需要导入 .jar 文件?
  2. 当我想导出自己的 java 库时,是否需要导出到 .jar 文件中。

Thanks for help in advance.

提前感谢您的帮助。

回答by Mike Clark

Is a .NET Assembly is same thing as java .jar?

.NET 程序集是否与 java .jar 相同?

They play the same role, yes.

他们扮演同样的角色,是的。

.dll contains compiled IL code and .jar contains complied .class/byte code files?

.dll 包含编译的 IL 代码,.jar 包含编译的 .class/字节码文件?

Yes. Although JARs are just zip files (you can open them in your favorite Zip tool), so they can really contain just about anything.

是的。尽管 JAR 只是 zip 文件(您可以在您最喜欢的 Zip 工具中打开它们),但它们实际上可以包含几乎任何内容。

They say resources, what kind of resources we are talking about here? Images, .txt files, etc. or all of them possible?

他们说资源,我们在这里谈论什么样的资源?图像、.txt 文件等或所有可能的文件?

Any file type is allowed in a JAR. Your Java code can access the contents of files within the jar via, for example, getResourceAsStreamor getResource. Obviously .class files in a JAR are treated specially (as bytecode).

JAR 中允许使用任何文件类型。您的 Java 代码可以通过例如getResourceAsStreamgetResource访问 jar 中文件的内容。显然,JAR 中的 .class 文件被特殊对待(作为字节码)。

I've examined AWS (Amazon Web Service API for java) and I saw three .jar file and they are: aws-java-sdk-1.1.1.jar, aws-java-sdk-1.1.1-javadoc.jar, aws-java-sdk-1.1.1-sources.jar

我检查了 AWS(用于 Java 的亚马逊网络服务 API),我看到了三个 .jar 文件,它们是:aws-java-sdk-1.1.1.jar、aws-java-sdk-1.1.1-javadoc.jar、 aws-java-sdk-1.1.1-sources.jar

The above packaging is fairly common. Sometimes people use .jaras an alternative file extension to .zip. I find this practice confusing, but it is not uncommon, and Amazon has done it in this case. I think this practice became more common with the launch of Maven, which stores reference source code in files named .jar.

上述包装相当普遍。有时人们使用.jar作为.zip的替代文件扩展名。我觉得这种做法令人困惑,但并不少见,亚马逊在这种情况下就做到了。我认为随着 Maven 的推出,这种做法变得更加普遍,Maven 将参考源代码存储在名为 .jar 的文件中。

aws-java-sdk-1.1.1.jar- This is the only file necessary for compilation and execution. It contains the classes (.class files) and resources necessary for the library to function.

aws-java-sdk-1.1.1.jar- 这是编译和执行所需的唯一文件。它包含库运行所需的类(.class 文件)和资源。

aws-java-sdk-1.1.1-sources.jar- This is just a zip file that contains the source code (.java files) for the library. It is not necessary for compilation or execution. It is provided to help you troubleshoot problems you may encounter while using the library. You could unzip this file and read the source code from your hard drive. However, your IDE can probably utilize this file directly by creating a "source attachment" to the main library JAR. With this library->source association set up in your IDE, your IDE will be able to show you the source code of the library when you use your IDE's "Go to Definition" feature on a library class.

aws-java-sdk-1.1.1-sources.jar- 这只是一个包含库的源代码(.java 文件)的 zip 文件。它不是编译或执行所必需的。提供它是为了帮助您解决使用库时可能遇到的问题。您可以解压缩此文件并从硬盘读取源代码。但是,您的 IDE 可能可以通过创建主库 JAR 的“源附件”来直接使用此文件。通过在 IDE 中设置此库-> 源关联,当您在库类上使用 IDE 的“转到定义”功能时,您的 IDE 将能够向您显示库的源代码。

aws-java-sdk-1.1.1-javadoc.jar- This is just a zip file that contains the JavaDoc for the library. Again, it is not necessary for compilation or execution. JavaDoc is just HTML, so you could unzip this file and read the JavaDoc HTML directly from your hard drive. However, most IDEs can create a "javadoc attachment" to the main library, which allows you to pull up context-sensitive JavaDoc for the library from within the IDE. Actually, most IDEs can generate the JavaDoc on the fly from the -sources.jar, so I rarely use javadoc jars anymore.

aws-java-sdk-1.1.1-javadoc.jar- 这只是一个包含库的 JavaDoc 的 zip 文件。同样,编译或执行不是必需的。JavaDoc 只是 HTML,因此您可以解压缩此文件并直接从硬盘读取 JavaDoc HTML。但是,大多数 IDE 可以为主库创建一个“javadoc 附件”,这允许您从 IDE 中为库提取上下文相关的 JavaDoc。实际上,大多数 IDE 可以从 -sources.jar 动态生成 JavaDoc,因此我很少再使用 javadoc jar。

When I want to import java libraries, will I always need to import .jar files?

当我想导入 java 库时,是否总是需要导入 .jar 文件?

It's usually called "adding JARs to the classpath", but yes. You pretty much always need to help Java find all the JARs you're using by constructing a classpath whenever you build or execute. In general, using JARs and classpaths is a much more manual and explicit process than using DLLs in .NET. For example: Java has no direct equivalent of .NET's "global assembly cache"; also vanilla Java will not automatically load .jars from the working directory.

它通常被称为“将 JAR 添加到类路径”,但是是的。无论何时构建或执行,您几乎总是需要通过构建类路径来帮助 Java 找到您正在使用的所有 JAR。通常,与在 .NET 中使用 DLL 相比,使用 JAR 和类路径是一个更加手动和明确的过程。例如:Java 没有直接等同于 .NET 的“全局程序集缓存”;此外,vanilla Java 不会自动从工作目录加载 .jars。

There are a few other techniques for creating a more "automatic" feeling classpath, but they are kind of advanced: Manifest.MF classpaths are possible, for example -- but this approach is used infrequently as it is brittle. As of Java 6, Java has limited support for using wildcards in the classpath.

还有一些其他技术可以创建更“自动”的感觉类路径,但它们有点高级:例如,Manifest.MF 类路径是可能的——但是这种方法很少使用,因为它很脆弱。从 Java 6 开始,Java 对在 classpath 中使用通配符的支持有限。

When I want to export my own java libraries, should I need to export in .jar file

当我想导出我自己的 java 库时,我是否需要在 .jar 文件中导出

This is standard, yes.

这是标准的,是的。

回答by Courtney Christensen

I can answer partof that question :-)

我可以回答这个问题的一部分:-)

As I understand it, a jaris just a "Java Archive" file. It can contain anything you want, but is most often used to distribute binary versions of libraries. It can also be used for distributing executables -- double-clicking a JAR file will launch the application, if that's how it's been packaged.

据我了解,ajar只是一个“Java 档案”文件。它可以包含您想要的任何内容,但最常用于分发库的二进制版本。它还可以用于分发可执行文件——双击 JAR 文件将启动应用程序,如果它是这样打包的。

As such, it may be difficult to tell what a particular jarfile is intended for. For example, what is aws-java-sdk-1.1.1-sources.jar?. Is it a self-installer? A compressed code archive? Without reading documentation, it's anybody's guess.

因此,可能很难判断特定jar文件的用途。例如,什么是aws-java-sdk-1.1.1-sources.jar?。它是自安装程序吗?压缩代码存档?没有阅读文档,这是任何人的猜测。



Edit:case in point, I just downloaded the JAIjarfile assuming it was a binary build of the library. But in fact, it contains the installer application.

编辑:举个例子,我刚刚下载了JAIjar文件,假设它是库的二进制版本。但实际上,它包含安装程序应用程序。

回答by Buhake Sindi

To answer this specific question.

要回答这个具体问题。

  1. aws-java-sdk-1.1.1.jar
  2. aws-java-sdk-1.1.1-javadoc.jar
  3. aws-java-sdk-1.1.1-sources.jar
  1. aws-java-sdk-1.1.1.jar
  2. aws-java-sdk-1.1.1-javadoc.jar
  3. aws-java-sdk-1.1.1-sources.jar

The 1) is the compiler java archive file (jar), which in .NET is called a DLL. The 2) is the javadoc. An HTML documentation of java classes (found in 1)) along with method description. The 3) is the source code (java files) of 1).

1) 是编译器 java 存档文件 (jar),在 .NET 中称为 DLL。2) 是javadoc。Java 类的 HTML 文档(见 1))以及方法描述。3)是1)的源代码(java文件)。

  1. When I want to import java libraries, will I always need to import .jar files?
  2. When I want to export my own java libraries, should I need to export in .jar file.
  1. 当我想导入 java 库时,是否总是需要导入 .jar 文件?
  2. 当我想导出自己的 java 库时,是否需要导出到 .jar 文件中。

For 1) If you need to use a class that is found in a jar file, then you'll need to import that jar into your project.

对于 1) 如果您需要使用在 jar 文件中找到的类,那么您需要将该 jar 导入到您的项目中。

For 2) You don't need to. You can bundle your dependency jars into your library or package each into their own jars and import them into your project.

对于 2) 你不需要。您可以将依赖项 jar 捆绑到您的库中,或者将每个 jar 打包到各自的 jar 中并将它们导入到您的项目中。

  1. .NET Assembly is same thing as java .jar? Answer:Yes.
  2. .dll contains compiled IL code and .jar contains complied .class/byte code files? Answer:.jar files is an archived file of java resources, classes, images/icons/etc. that is necessary for an application, just like in .DLL. Class files contains compile java source codes (Called java bytecode).
  1. .NET Assembly 和 java .jar 是一样的吗? 回答:是的。
  2. .dll 包含编译的 IL 代码,.jar 包含编译的 .class/字节码文件? 答:.jar 文件是java 资源、类、图像/图标/等的归档文件。这是应用程序所必需的,就像在 .DLL 中一样。类文件包含编译java源代码(称为java字节码)。