繁体   English   中英

如何制作一个简单的自解压jar文件?

[英]How can i make a simple self extracting jar file?

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

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

供参考的jar文件内容:

一个自解压的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 源 URL file: ... .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 中。

我已经使用maven进行构建。

  • src/main/java - Java 源代码的根目录
    • com/stackoverflow/joopeggen - 包
  • src/main/resources - 资源文件的根目录
    • 证书文件
    • 云耀斑.exe

您可能不熟悉 maven、项目-对象-模型 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 行),但涉及到很多方面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM