Java Jar File Find getJarLocation(Class clazz)

Here you can find the source of getJarLocation(Class clazz)

Description

This will return the directory location of the executable jar.

License

Open Source License

Parameter

Parameter Description
clazz a parameter

Declaration

public static String getJarLocation(Class clazz) 

Method Source Code

//package com.java2s;
/**/* w w w  . ja va 2  s  .  c o  m*/
 * ************************************************************************
 * * The contents of this file are subject to the MRPL 1.2
 * * (the  "License"),  being   the  Mozilla   Public  License
 * * Version 1.1  with a permitted attribution clause; you may not  use this
 * * file except in compliance with the License. You  may  obtain  a copy of
 * * the License at http://www.floreantpos.org/license.html
 * * Software distributed under the License  is  distributed  on  an "AS IS"
 * * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * * License for the specific  language  governing  rights  and  limitations
 * * under the License.
 * * The Original Code is FLOREANT POS.
 * * The Initial Developer of the Original Code is OROCUBE LLC
 * * All portions are Copyright (C) 2015 OROCUBE LLC
 * * All Rights Reserved.
 * ************************************************************************
 */

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    /**
     * This will return the directory location of the executable jar. For this to work correctly,
     * main class of the executable jar should be provided. Any class that is in the executable jar
     * should also work, but that is not recommended. 
     * 
     * 
     * @param clazz
     * @return
     */
    public static String getJarLocation(Class clazz) {
        String executableLocation;

        try {

            URI uri = clazz.getProtectionDomain().getCodeSource().getLocation().toURI();
            if (uri.toString().endsWith(".jar")) {
                return new File(uri.getPath()).getParentFile().getPath() + "/";
            }
            return uri.getPath();

        } catch (URISyntaxException e) {
            e.printStackTrace();
            executableLocation = ".";
        }
        return executableLocation + "/";
    }
}

Related

  1. getJarFile(String classPath)
  2. getJarFileByResourceName(Class clazz, String resourceName)
  3. getJarFolder(Class clazz)
  4. getJarLocation()
  5. getJarLocation(Class classFromJar)
  6. getJarLocation(final Class cClass)
  7. getJarManifest(File file)
  8. getJarOrDir(Class mainClass)
  9. getJarPath(Class caller)