0

basically i need a jar installer, that extracts cert.pem and cloudflare.exe from itself into a temporary directory, and withnout using any 3rd party libraries.

I tried to find some similar questions, but i only found outdated questions, or questions that werent useful.

Contents of the jar file for reference:

Damca
  • 50
  • 1
  • 8
  • A jar file is a zip. There is in java a `jar:file:` zip file system, where you can use a Path and do a `Files.copy` to where the jar resides for instance. So create an executable jar with a manifest with Main-Class and so on. – Joop Eggen Nov 15 '21 at 18:37
  • @JoopEggen how can i do this exactly? I dont have much experience with java – Damca Nov 15 '21 at 18:57
  • @M.Prokhorov not really, i need the jar file to extract files from itself, not from other jar. But it still might be useful in my other projects, thanks. – Damca Nov 15 '21 at 19:02
  • Extracting from the host jar is just a special case of extracting from any other Jar, so how is that question not useful? Just pass it your own host jar and problem solved. – M. Prokhorov Nov 15 '21 at 19:06
  • But if that one doesn't fit, how about [this one](https://stackoverflow.com/questions/16010539/getresourceasstream-filepath-while-running-jar)? – M. Prokhorov Nov 15 '21 at 19:18
  • @M.Prokhorov hmm, the second one is better, but can you please tell me how can i write the output from `getResourceAsStream` to a file? Also, sorry for being dumb – Damca Nov 15 '21 at 20:01
  • @M.Prokhorov nevermind, i figured it out. Thank you very much for your help :) – Damca Nov 15 '21 at 22:08
  • I have added the code for a self-extracting jar that extracts all files in the root of the jar. – Joop Eggen Nov 16 '21 at 00:12

1 Answers1

1

A self-extracting jar (still needing a java installation)

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);
        }
    }

The extraction uses class information to get the jar source 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);
                    }
                });
    }
}

The main class needs to be inside the META-INF/MANIFEST.MF.

I have used maven for the build.

  • src/main/java - the root for java sources
    • com/stackoverflow/joopeggen - the package
  • src/main/resources - the root for resource files
    • cert.pem
    • cloudflare.exe

As you might not be familiar with maven, the 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>

The code only requires a few lines (approx. 17), though many aspects are touched.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138