Skip to content

JarUtils

Fulminazzo edited this page Oct 2, 2023 · 1 revision

JarUtils is a group of function created to easily work with jar files and their contents. Sometimes it can be useful to access the contents of a jar file, whether it is a class or a resource, for example to get the default instance of a config.yml file.
In any case, JarUtils allows to do any of that:

public class JarUtils {
    
    /**
     * Returns the file at the specified path from the given File.
     * @param file: the File (should be a .jar);
     * @param path: the path.
     * @return an InputStream with the file contents (if found).
     */
    public static InputStream getJarFile(File file, String path);

    /**
     * Returns the file at the specified path from the given JarFile.
     * @param jar: the JarFile;
     * @param path: the path.
     * @return an InputStream with the file contents (if found).
     */
    public static InputStream getJarFile(JarFile jar, String path);

    /**
     * Returns an instance of JarFile from the given Class.
     * @param jarClass: the Class.
     * @return the corresponding JarFile, if present.
     */
    public static JarFile getJarFile(Class<?> jarClass);

    /**
     * Returns an instance of JarFile from the given path.
     * @param jarPath: the String path.
     * @return the corresponding JarFile, if present.
     */
    public static JarFile getJar(String jarPath);

    /**
     * Returns an instance of JarFile from the given File.
     * @param jarFile: the File.
     * @return the corresponding JarFile, if present.
     */
    public static JarFile getJar(File jarFile);
}