关于 .jar 文件的一点困惑

发布于 2024-12-04 04:16:46 字数 468 浏览 1 评论 0原文

在计算机科学中,我了解到 .jar 文件基本上是已编译的 .java 文件的压缩集。因此,当您有一个项目时,您可以拥有一堆压缩类(一个 .jar),而不是那 20 个 .java 文件。去年在 CSI 中,我们使用了一个名为 DanceStudio 的 .jar 文件,我们必须使用它来让脚在地板上行走。今年,我们正在使用不同的程序来更好地理解 java,因此我解压了包含 26 个类的 .jar 文件,然后对其进行了反编译。我想尝试通过编译所有 .java 文件以及使程序运行所需的其他文件(Walker、Foot 等)来创建一个程序。当我尝试编译所有这些文件时,它会说我有重复的文件文件(Walker、Foot 等)我不明白的是,如果 .jar 文件基本上是相同的东西,只是采用压缩形式,为什么会编译。同样让我困惑的是.jar 中的 Foot、ETC 文件实际上更复杂并且有更多代码。

有人可以解释 .jar 文件实际上如何工作并将这些文件分开,以及它如何与 .jar 文件中不存在的重复类一起运行吗?

In computer science I have learned that .jar files are basically a compressed set of .java files that have been compiled. So, when you have a project, instead of those 20 .java files you can have a pile of compressed classes (a .jar). Last year in CSI we worked with a .jar file called DanceStudio, which we had to use to make feet walk across the floor. This year, we are working with a different program to better understand java, so i unzipped the .jar file contained 26 classes, which I then decompiled. I wanted to try to create a program by compiling all of the .java files with the others necessary to make the program run (Walker, Foot, ETC.) When I try to compile all of these files, it will say that I have duplicate files (Walker, Foot, ETC.) What I don't understand is why this would compile if the .jar file was basically the same thing, just in a compressed form. What also confuses me is that the Foot, ETC files in the .jar are actually more complicated and have more code.

Could someone please explain how the .jar file actually works and separates these files apart, and how it could run with a duplicate class that isn't in the .jar file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

望她远 2024-12-11 04:16:46

首先,您在解释 .jar 文件时遗漏了一个步骤。

.jar 文件是 .class 文件的集合。 .class 文件是通过编译 .java 文件生成的。

通常单个 .java 文件将生成单个 .class 文件,因为它将包含单个类型定义。但是一个 .java 文件可以通过多种方式生成多个 .class 文件(内部/嵌套类、匿名类、顶级非公共类、. ..),因此 .java 文件和 .class 文件之间不一定是一对一的关联。

还有一个令人困惑的问题是,为什么反编译后的 Java 源代码看起来比原始 Java 源代码更复杂。这个问题很容易回答:编译步骤并不是被设计为可逆的。

当 Java 编译器将 .java 文件转换为 .class 文件时,它会生成最适合执行的格式。该格式不会代表与 Java 源文件完全相同的概念。例如:Java 字节码中没有经典的“if”。它将被执行适当的跳转命令。

所有这些意味着将 .class 文件转换回 .java 文件的过程非常复杂,而且通常并不完美。

First of all, you're missing one step in your explanation of a .jar file.

A .jar file is a collection of .class files. And .class files are what is produced by compiling a .java file.

Usually a single .java file will produce a single .class file, because it will contain a single type definition. But there are several ways for a .java file to produce more than one .class files (inner/nested classes, anonymous classes, top-level non-public classes, ...), so it's not necessarily a 1-to-1 association between .java files and .class files.

Then there's the confusion why the decompiled Java source code looks more complicated than the original Java source. This one is easy to answer: the compilation step was not designed to be reversable.

When the Java compiler turns .java files to .class files it produces a format that is best suited for being executed. That format will not represent the exact same concepts that the Java source file does. For example: there's no classical "if" in the Java bytecode. It will be implemented be appropriate jump commands.

All of this means that the process of converting .class files back to .java files is complicated and usually non-perfect.

不知所踪 2024-12-11 04:16:46
  1. 您通常将(明文).java 源文件编译为(二进制).class 文件。

  2. 如果您使用包,则类文件将位于不同的子目录(代表包)中。

  3. .jar 文件是一种压缩的二进制文件,它将正确目录中的所有 .class 放入一个紧凑且易于管理的文件中。

  4. .jar 文件还可以包含其他文件,例如清单、位图和资源。

  5. .jar 文件也可以被“签名”以确保其内容的完整性/真实性。

以下是一些很好的链接:

http://en.wikipedia.org/wiki/JAR_%28file_format %29

http://download.oracle.com/javase/tutorial/deployment/jar/

'希望有帮助

  1. You generally compile your (clear text) .java source files into (binary) .class files.

  2. If you use packages, then the class files will be in different subdirectories (representing the package).

  3. A .jar file is a compressed binary file that puts all the .classes in the right directories in one compact, easy to manage file.

  4. .jar file can also contain other files, such as manifests, bitmaps and resources.

  5. .jar files can also be "signed" to insure the integrity/authenticity of their contents.

Here are some good links:

http://en.wikipedia.org/wiki/JAR_%28file_format%29

http://download.oracle.com/javase/tutorial/deployment/jar/

'Hope that helps

百合的盛世恋 2024-12-11 04:16:46

关于您的重复项:也许您的 .jar 仍在构建路径中,因此当您尝试使用反编译的类编译项目时,您将有重复项。检查并删除 .jar(如果它仍在您的构建路径中)。

About your duplicate: Maybe your .jar is still in your build path, so when you try to compile your project with the decompiled class, you will have duplicate. check and remove the .jar if its still in your build path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文