Creating JAR Files Programmatically – 以编程方式创建JAR文件

最后修改: 2022年 6月 24日

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

1. Introduction

1.介绍

In this brief article, we’ll go over the process of creating jar files programmatically. When writing software, eventually we need to deploy it into a production state. In some cases, it is okay to use a classpath with separate files. Usually, it is more convenient to handle a single file. In the case of Java, the standard way of doing this is with a JAR, WAR, or EAR file.

在这篇简短的文章中,我们将介绍以编程方式创建jar文件的过程。在编写软件时,最终我们需要将其部署到生产状态。在某些情况下,使用一个带有独立文件的classpath是可以的。通常情况下,处理一个文件会更方便。在Java的情况下,标准的方法是使用JAR、WAR或EAR文件。

The basic process is to write the manifest, open the jar, add the contents, and finally, close the jar.

基本过程是编写清单,打开jar,添加内容,最后关闭jar。

2. Anatomy of a Jar File

2.剖析罐状文件

A jar file is an extension of the ZIP file format, with the inclusion of a manifest file. The manifest file is a special file specific to JAR files and may contain various settings. Some of these are the main class, optional data (ie, author, version, etc.), and code signing information.

jar文件是ZIP文件格式的扩展,其中包含一个清单文件。清单文件是JAR文件特有的一个特殊文件,可能包含各种设置。其中一些是主类、可选数据(即作者、版本等)和代码签名信息。

We may use zip-compatible tools, such as WinRar, to view and extract some or all of an archive. We can also include a jars or libs subdirectory for containing dependency jars. Since a jar is an extension of zip files, we can include any file or directory.

我们可以使用兼容zip的工具,例如WinRar,来查看和提取部分或全部的存档。我们也可以包括一个jars或libs子目录,以包含依赖性jar。由于jar是zip文件的扩展,我们可以包括任何文件或目录。

3. Creating a JarTool Class

3.创建一个JarTool

To simplify the process of creating a JAR file, we create a solitary, Plain Old Java Object (POJO) class that encapsulates our operations. We may include putting entries in a manifest file, creating a JAR file, adding files or directories.

为了简化创建JAR文件的过程,我们创建了一个单独的、普通的旧Java对象(POJO)类,封装了我们的操作。我们可能包括将条目放入清单文件、创建JAR文件、添加文件或目录。

We can also create methods to perform deletions from a JAR or even append entries to an existing JAR, although these operations require fully reading and re-writing the JAR.

我们还可以创建方法来执行从JAR中删除,甚至向现有的JAR追加条目,尽管这些操作需要完全读取和重写JAR。

3.1. JAR Manifest

3.1.JAR Manifest

In order to create a JAR file, we must first begin the manifest:

为了创建一个JAR文件,我们必须首先启动清单。

public class JarTool {    
    private Manifest manifest = new Manifest();

    public void startManifest() {
        manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    }
}

If we want the jar to be executable, we must set the main class:

如果我们希望jar是可执行的,我们必须设置主类。

public void setMainClass(String mainFQCN) {
    if (mainFQCN != null && !mainFQCN.equals("")) {
        manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainFQCN);
    }
}

Also, if we want to specify additional attributes, we can add them to the manifest, for example:

另外,如果我们想指定额外的属性,我们可以把它们添加到清单中,比如说。

addToManifest("Can-Redefine-Classes", "true");

Here is that method:

下面是这个方法。

public void addToManifest(String key, String value) {
     manifest.getMainAttributes().put(new Attributes.Name(key), value);
}

3.2. Opening the Jar for Writing

3.2.打开罐子进行写作

With the manifest completed, we may now write the entries to the JAR file. To do this, we must first open the jar:

清单完成后,我们现在可以把条目写到JAR文件中。要做到这一点,我们必须首先打开jar文件。

public JarOutputStream openJar(String jarFile) throws IOException {        
    return new JarOutputStream(new FileOutputStream(jarFile), manifest);
}

3.3. Adding Files to the Jar

3.3.将文件添加到罐子中

When adding files to the JAR, Java uses the Solaris style filenames using a forward slash as a separator (/). Note that we can add any file of any type, including other JAR files or empty directories. This is really handy for including dependencies.

在向JAR添加文件时,Java使用Solaris风格的文件名,使用正斜杠作为分隔符(/)。注意,我们可以添加任何类型的文件,包括其他JAR文件或空目录。这对于包括依赖关系来说真的很方便。

Also, because the JAR file is a form of a classpath, we must specify what part of the absolute path we wish to use inside the JAR. For our purposes, the root path will be our project’s classpath.

此外,由于JAR文件是classpath的一种形式,我们必须指定我们希望在JAR内使用绝对路径的哪一部分。对于我们的目的,根路径将是我们项目的classpath。

Understanding this, we can now finish our JarTool class with this method:

了解了这一点,我们现在可以用这个方法完成我们的JarTool类。

public void addFile(JarOutputStream target, String rootPath, String source) 
  throws FileNotFoundException, IOException {
    String remaining = "";
    if (rootPath.endsWith(File.separator)) {
        remaining = source.substring(rootPath.length());
    } else {
        remaining = source.substring(rootPath.length() + 1);
    }
    String name = remaining.replace("\\","/");
    JarEntry entry = new JarEntry(name);
    entry.setTime(new File(source).lastModified());
    target.putNextEntry(entry);
    
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
    byte[] buffer = new byte[1024];
    while (true) {
        int count = in.read(buffer);
        if (count == -1) {
            break;
        }
        target.write(buffer, 0, count);
    }
    target.closeEntry();
    in.close();
}

4. A Working Example

4.一个工作实例

To demonstrate the minimum requirement for an executable jar, we’ll write an application class and then see how it works:

为了演示可执行的jar的最低要求,我们将编写一个应用类,然后看看它是如何工作的。

public class Driver {
    public static void main(String[] args) throws IOException {
        JarTool tool = new JarTool();
        tool.startManifest();
        tool.addToManifest("Main-Class", "com.baeldung.createjar.HelloWorld");

        JarOutputStream target = tool.openJar("HelloWorld.jar");
        
        tool.addFile(target, System.getProperty("user.dir") + "\\src\\main\\java",
          System.getProperty("user.dir") + "\\src\\main\\java\\com\\baeldung\\createjar\\HelloWorld.class");
        target.close();
    }
}

The HelloWorld class is a very simple class with a single main() method that prints out the text:

HelloWorld类是一个非常简单的类,只有一个main()方法可以打印出文本。

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

And to demonstrate that it works, we have this example:

为了证明它的作用,我们有这个例子。

$ javac -cp src/main/java src/main/java/com/baeldung/createjar/HelloWorld.java
$ javac -cp src/main/java src/main/java/com/baeldung/createjar/JarTool.java
$ javac -cp src/main/java src/main/java/com/baeldung/createjar/Driver.java
$ java -cp src/main/java com/baeldung/createjar/Driver
$ java -jar HelloWorld.jar
Hello World!

Here, we’ve compiled each class, then executed the Driver class, which will create the HelloWorld jar. Finally, we’ve executed the jar, which results in printing the “Hello World” message.

在这里,我们已经编译了每个类,然后执行了Driver类,这将创建HelloWorld jar。最后,我们执行了这个jar,结果是打印出了 “Hello World “信息。

The commands above should be executed from the project location.

上面的命令应该从项目所在地执行。

5. Conclusion

5.总结

In this tutorial, we saw how to create a jar file programmatically, add files to it, and finally execute it.

在本教程中,我们看到如何以编程方式创建一个jar文件,向其中添加文件,最后执行它。

And, of course, the code is available over on GitHub.

当然,代码也可以在GitHub上获得