All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.jpackage.DescribeModule Maven / Gradle / Ivy

The newest version!
package net.dongliu.jpackage;

import net.dongliu.jpackage.util.ModuleInfo;
import net.dongliu.jpackage.util.ProcessResult;
import net.dongliu.jpackage.util.ProcessUtils;

import java.nio.file.Path;

/**
 * @author dongliu
 */
public class DescribeModule {
    private final Path jarPath;
    private final Path jmodPath;
    private final Path path;

    public DescribeModule(Path jarPath, Path jmodPath, Path path) {
        this.jarPath = jarPath;
        this.jmodPath = jmodPath;
        this.path = path;
    }

    // return mod name
    public ModuleInfo describe() {
        String suffix = path.getFileName().toString().split("\\.")[1];
        switch (suffix) {
            case "jar":
                return describeJar();
            case "jmod":
                return describeJmod();
            default:
                throw new RuntimeException("unknown module type: " + suffix);
        }
    }

    private ModuleInfo describeJar() {
        ProcessResult result = ProcessUtils.execute(jarPath.toString(), "--file", path.toString(), "--describe-module");
        if (result.exitCode() != 0) {
            return null;
        }
        String name = result.stdout().split(" ")[0];
        if (name.isEmpty()) {
            return null;
        }
        String mainClass = null;
        for (String line : result.stdout().split("\n")) {
            String[] items = line.trim().split("\\s+");
            if (items.length == 2) {
                if (items[0].trim().equals("main-class")) {
                    mainClass = items[1].trim();
                }
            }
        }
        return new ModuleInfo(name, mainClass);
    }

    private ModuleInfo describeJmod() {
        ProcessResult result = ProcessUtils.execute(jmodPath.toString(), "describe", path.toString());
        if (result.exitCode() != 0) {
            return null;
        }
        String name = result.stdout().split("\n")[0].trim();
        name = name.split("@")[0];
        if (name.isEmpty()) {
            return null;
        }
        return new ModuleInfo(name, null);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy