【问题标题】:How can i make a simple self extracting jar file? [duplicate]如何制作一个简单的自解压 jar 文件? [复制]
【发布时间】:2021-12-26 22:37:57
【问题描述】:

基本上我需要一个 jar 安装程序,它将 cert.pemcloudflare.exe 从自身提取到一个临时目录中,并且不使用任何第 3 方库。

我试图找到一些类似的问题,但我只发现了过时的问题,或者没有用的问题。

供参考的jar文件内容:

【问题讨论】:

  • jar 文件是 zip。在 java 中有一个 jar:file: zip 文件系统,您可以在其中使用 Path 并对 jar 所在的位置执行 Files.copy 。因此,使用 Main-Class 等创建一个带有清单的可执行 jar。
  • @JoopEggen 我该怎么做呢?我对java没有太多经验
  • @M.Prokhorov 不是真的,我需要 jar 文件从自身提取文件,而不是从其他 jar 中提取文件。但它在我的其他项目中仍然可能有用,谢谢。
  • 从宿主 jar 中提取只是从任何其他 jar 中提取的一种特殊情况,那么这个问题怎么没用呢?只需将您自己的主机 jar 传递给它,问题就解决了。
  • 但是如果那个不合适,this one 怎么样?

标签: java jar


【解决方案1】:

一个自解压的jar(还需要安装java)

package com.stackoverflow.joopeggen;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.*;
import java.security.ProtectionDomain;
import java.util.Map;

public class App {

    public static void main(String[] args) {
        try {
            new App().extractSelf();
        } catch (IOException | URISyntaxException| IllegalStateException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

提取使用类信息获取jar源URLfile: ... .jar

    private void extractSelf() throws IOException, URISyntaxException {
        ProtectionDomain protectionDomain = App.class.getProtectionDomain();
        URL fileUrl = protectionDomain.getCodeSource().getLocation();
        // "file: ... .jar"
        Path jarDir = Paths.get(fileUrl.toURI()).getParent();
        URI jarFileUri = URI.create("jar:" + fileUrl);
        Map<String, Object> env = Map.of("Encode", "UTF-8");
        FileSystem zipFS = FileSystems.newFileSystem(jarFileUri, env);
        Path root = zipFS.getPath("/");
        Files.list(root)
                .filter(path -> Files.isRegularFile(path))
                .forEach(path -> {
                    try {
                        Path extractedPath = jarDir.resolve(path.getFileName().toString());
                        System.out.println("* " + extractedPath);
                        Files.copy(path, extractedPath);
                    } catch (IOException e) {
                        throw new IllegalStateException(path.getFileName().toString(), e);
                    }
                });
    }
}

主类需要在 META-INF/MANIFEST.MF 中。

我使用 ma​​ven 进行构建。

  • src/main/java - java 源的根目录
    • com/stackoverflow/joopeggen - 包
  • src/main/resources - 资源文件的根目录
    • cert.pem
    • cloudflare.exe

您可能不熟悉 maven,Project-Object-Model XML,pom.xml。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.stackoverflow.joopeggen</groupId>
<artifactId>selfextracting</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <!-- exec:exec to start the jar instead of generated classes. -->
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>maven</executable>
            </configuration>
        </plugin>
        <plugin>
            <!-- To fill META-INF/MANIFEST.MF with Main-Class. -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.stackoverflow.joopeggen.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

代码只需要几行(大约 17 行),但涉及到很多方面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-03
    • 2020-02-28
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2019-03-19
    相关资源
    最近更新 更多