Viewing Contents of a JAR File – 查看JAR文件的内容

最后修改: 2020年 12月 17日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

We’ve learned about getting class names from a JAR file. Further, in that tutorial, we’ve discussed how to get the classes’ names in a JAR file in a Java application.

我们已经了解了从JAR文件中获取类的名称。此外,在该教程中,我们还讨论了如何在Java应用程序中获取JAR文件中的类名。

In this tutorial, we’ll learn another way to list a JAR file’s content from the command-line.

在本教程中,我们将学习另一种从命令行列出JAR文件内容的方法。

We’ll also see several GUI tools for viewing more detailed contents of a JAR file — for example, the Java source code.

我们还将看到几个用于查看JAR文件更详细内容的GUI工具–例如,Java源代码。

2. Example JAR File

2.示例JAR文件

In this tutorial, we’ll still take the stripe-0.0.1-SNAPSHOT.jar file as an example to address how to view the content in a JAR file:

在本教程中,我们仍将以stripe-0.0.1-SNAPSHOT.jar文件为例,解决如何查看JAR文件中的内容。

3. Reviewing the jar Command

3.审查jar命令

We’ve learned that we can use the jar command shipped with the JDK to check the content of a JAR file:

我们已经知道,我们可以使用JDK附带的jar命令来检查JAR文件的内容。

$ jar tf stripe-0.0.1-SNAPSHOT.jar 
META-INF/
META-INF/MANIFEST.MF
...
templates/result.html
templates/checkout.html
application.properties
com/baeldung/stripe/StripeApplication.class
com/baeldung/stripe/ChargeRequest.class
com/baeldung/stripe/StripeService.class
com/baeldung/stripe/ChargeRequest$Currency.class

If we want to filter the output to get only the information we want, for example, class names or properties files, we can pipe the output to filter tools such as grep.

如果我们想过滤输出,只得到我们想要的信息,例如,类名或属性文件,我们可以将输出管道到过滤工具,如grep

The jar command is pretty convenient to use if our system has a JDK installed.

如果我们的系统安装了JDK,jar命令的使用就相当方便。

However, sometimes, we want to examine a JAR file’s content on a system without a JDK installed. In this case, the jar command is not available.

然而,有时我们想在没有安装JDK的系统上检查一个JAR文件的内容。在这种情况下,jar命令是不可用的。

We’ll take a look at this next.

我们接下来看一下这个问题。

4. Using the unzip Command

4.使用unzip命令

JAR files are packaged in the ZIP file format. In other words, if a utility can read a ZIP file, we can use it to view a JAR file as well.

JAR文件是以ZIP文件格式打包的。换句话说,如果一个工具可以读取ZIP文件,我们也可以用它来查看JAR文件。

The unzip command is a commonly used utility for working with ZIP files from the Linux command-line.

unzip命令是一个常用的工具,用于从Linux命令行处理ZIP文件。

Therefore, we can use the -l option of the unzip command to list the content of a JAR file without extracting it:

因此,我们可以使用unzip命令的-l选项来列出一个JAR文件的内容,而不提取它。

$ unzip -l stripe-0.0.1-SNAPSHOT.jar
Archive:  stripe-0.0.1-SNAPSHOT.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2020-10-16 20:53   META-INF/
...
      137  2020-10-16 20:53   static/index.html
      677  2020-10-16 20:53   templates/result.html
     1323  2020-10-16 20:53   templates/checkout.html
       37  2020-10-16 20:53   application.properties
      715  2020-10-16 20:53   com/baeldung/stripe/StripeApplication.class
     3375  2020-10-16 20:53   com/baeldung/stripe/ChargeRequest.class
     2033  2020-10-16 20:53   com/baeldung/stripe/StripeService.class
     1146  2020-10-16 20:53   com/baeldung/stripe/ChargeRequest$Currency.class
     2510  2020-10-16 20:53   com/baeldung/stripe/ChargeController.class
     1304  2020-10-16 20:53   com/baeldung/stripe/CheckoutController.class
...
---------                     -------
    15394                     23 files

Thanks to the unzip command, we can view the content of a JAR file without the JDK.

多亏了unzip命令,我们可以在没有JDK的情况下查看JAR文件的内容。

The output above is pretty clear. It lists the files in the JAR file in a tabular format.

上面的输出是相当清楚的。它以表格的形式列出了JAR文件中的文件。

5. Exploring JAR Files Using GUI Utilities

5.使用GUI工具探索JAR文件

Both the jar and the unzip commands are handy, but they only list the filenames in a JAR file.

jarunzip命令都很方便,但它们只列出JAR文件中的文件名。

Sometimes, we would like to know more information about files in the JAR file, for example, examining the Java source code of a class.

有时,我们想知道JAR文件中文件的更多信息,例如,检查一个类的Java源代码。

In this section, we’ll introduce several platform-independent GUI tools to help us to look at files inside a JAR file.

在本节中,我们将介绍几个独立于平台的GUI工具,以帮助我们查看JAR文件中的文件。

5.1. Using JD-GUI

5.1.使用JD-GUI

First, let’s have a look at JD-GUI.

首先,让我们看一下JD-GUI

The JD-GUI is a nice open-source GUI utility to explore Java source code decompiled by the Java decompiler JD-Core.

JD-GUI是一个很好的开源GUI工具,用于探索由Java反编译器JD-Core反编译的Java源代码。

JD-GUI ships a JAR file. We can start the utility by using the java command with the -jar option, for instance:

JD-GUI提供了一个JAR文件。例如,我们可以通过使用带有 -jar选项的java命令来启动该工具。

$ java -jar jd-gui-1.6.6.jar

When we see the main window of JD-GUI, we can either open our JAR file by navigating the menu “File -> Open File…” or just drag-and-drop the JAR file in the window.

当我们看到JD-GUI的主窗口时,我们可以通过导航菜单”File -> Open File…“打开我们的JAR文件,或者直接将JAR文件拖放到窗口中。

Once we open a JAR file, all the classes in the JAR file will be decompiled.

一旦我们打开一个JAR文件,JAR文件中的所有类将被反编译。

Then we can select the files we’re interested in on the left side to examine their source code:

然后,我们可以在左侧选择我们感兴趣的文件来检查其源代码。

As we can see in the above demo, in the outline on the left side, the classes and the members of each class such as methods and fields are listed, too, just like we usually see in an IDE.

正如我们在上面的演示中看到的,在左边的大纲中,类和每个类的成员,如方法和字段也被列出,就像我们通常在IDE中看到的那样

It’s pretty handy to locate methods or fields, particularly when we need to check some classes with many lines of code.

这对于定位方法或字段是相当方便的,特别是当我们需要检查一些有很多行代码的类。

When we click through different classes on the left side, each class will be opened in a tab on the right side.

当我们在左侧点击不同的班级时,每个班级都会在右侧的标签中打开。

The tab feature is helpful if we need to switch among several classes.

如果我们需要在几个班级之间切换,标签功能很有帮助。

5.2. Using Jar Explorer

5.2.使用 Jar Explorer

Jar Explorer is another open-source GUI tool for viewing the contents of JAR files. It ships a jar file and a start script “Jar Explorer.sh“. It also supports the drag-and-drop feature, making opening a JAR file pretty easy.

Jar Explorer是另一个开源的GUI工具,用于查看JAR文件的内容。它提供了一个jar文件和一个启动脚本”Jar Explorer.sh“。它还支持拖放功能,使打开JAR文件变得相当容易。

Another nice feature provided by Jar Explorer is that it supports three different Java decompilers: JD-Core, Procyon, and Fernflower.

Jar Explorer 提供的另一个很好的功能是,它支持三种不同的 Java 反编译器。JD-Core、ProcyonFernflower

We can switch among the decompilers when we examine source code:

当我们检查源代码时,我们可以在反编译器之间进行切换。

Jar Explorer is pretty easy to use. The decompiler switching feature is nice, too. However, the outline on the left side stops at the class level.

Jar Explorer是相当容易使用的。反编译器的切换功能也很好。然而,左边的轮廓在类的层面上就停止了。

Also, since Jar Explorer doesn’t provide the tab feature, we can only open a single file at a time.

另外,由于Jar Explorer不提供标签功能,我们一次只能打开一个文件。

Moreover, every time we select a class on the left side, the class will be decompiled by the currently selected decompiler.

此外,每当我们在左侧选择一个类时,该类就会被当前选择的反编译器反编译。

5.3. Using Luyten

5.3.使用Luyten

Luyten is a nice open-source GUI utility for Java decompiler Procyon that provides downloads for different platforms, for example, the .exe format and the JAR format.

Luyten是一个不错的开源GUI工具,用于Java反编译器Procyon,提供不同平台的下载,例如,.exe格式和JAR格式。

Once we’ve downloaded the JAR file, we can start Luyten using the java -jar command:

一旦我们下载了JAR文件,我们就可以使用java -jar命令启动Luyten。

$ java -jar luyten-0.5.4.jar 

We can drag and drop our JAR file into Luyten and explore the contents in the JAR file:

我们可以将我们的JAR文件拖放到Luyten中,并探索JAR文件中的内容。

Using Luyten, we cannot choose different Java decompilers. But, as the demo above shows, Luyten provides various options for decompiling. Also, we can open multiple files in tabs.

使用Luyten,我们不能选择不同的Java反编译器。但是,正如上面的演示所示,Luyten提供了各种反编译的选项。此外,我们还可以在标签中打开多个文件。

Apart from that, Luyten supports a nice theme system, and we can choose a comfortable theme while examining the source codes.

除此之外,Luyten支持一个不错的主题系统,我们可以在检查源代码时选择一个舒适的主题。

However, Luyten lists the structure of the JAR file only to the file level.

然而,Luyten只列出了JAR文件的结构到文件级别。

6. Conclusion

6.结语

In this article, we’ve learned how to list files in a JAR file from the command-line. Later, we’ve seen three GUI utilities to view more detailed contents of a JAR file.

在这篇文章中,我们已经学会了如何从命令行中列出JAR文件中的文件。后来,我们看到了三个GUI工具,可以查看JAR文件的更详细内容。

If we want to decompile the classes and examine the JAR file’s source code, picking a GUI tool may be the most straightforward approach.

如果我们想对类进行反编译并检查JAR文件的源代码,挑选一个GUI工具可能是最直接的方法。