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

uk.org.retep.util.app.ApplicationUtil Maven / Gradle / Ivy

There is a newer version: 10.6
Show newest version
/*
 * 

Copyright (c) 1998-2007, Peter T Mount
* All rights reserved.

* *

Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met:

* *
    *
  • Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer.
  • * *
  • Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution.
  • * *
  • Neither the name of the retep.org.uk nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission.
  • * *
* *

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/ package uk.org.retep.util.app; import java.io.File; import java.net.MalformedURLException; import java.net.URL; /** * * @author peter */ public final class ApplicationUtil { private ApplicationUtil() { } /** * Return the jar file URL containing the given class * * @param clazz Class who's jar URL is required * @return URL * @throws MalformedURLException on error */ public static URL getJarURL( Class clazz ) throws MalformedURLException { URL classURL = clazz.getResource( clazz.getSimpleName() + ".class" ); if( classURL==null ) { return null; } String path = classURL.toString(); int i=path.indexOf('!'); if( i>-1 ) { path=path.substring( 0, i ); } if( path.startsWith( "jar:" ) ) { path = path.substring( 4 ); } return new URL( path ); } /** * Return a java.io.File representing the jar containing the supplied Class. *

Note This only works for applications

* @param clazz Class * @return File containing the Class, null if not resolvable */ public static File getJarFile( Class clazz ) { try { URL jarURL = getJarURL( clazz ); if( jarURL!=null ) { return new File( jarURL.getPath() ).getAbsoluteFile(); } return null; } catch( MalformedURLException ex ) { ex.printStackTrace(); return null; } } /** * Return a java.io.File representing the directory containing the jar * containing the supplied Class - in otherwords the application's * installation directory. * *

Note This only works for applications

* @param clazz Class * @return File representing the applications installation directory or * null if not resolvable */ public static File getApplicationDirectory( Class clazz ) { File jarFile = getJarFile( clazz ); if( jarFile==null ) { return null; } jarFile = jarFile.getParentFile(); // If the directory is lib then return it's parent if( jarFile!=null && "lib".equals( jarFile.getName() ) ) { jarFile = jarFile.getParentFile(); } return jarFile; } /** * Return a java.net.URL representing the directory containing the jar * containing the supplied Class - in otherwords the application's * installation directory. * *

Note This only works for applications

* @param clazz Class * @return URL representing the applications installation directory or * null if not resolvable */ public static URL getApplicationDirectoryURL( Class clazz ) { File dir = getApplicationDirectory( clazz ); try { return dir==null ? null : dir.toURI().toURL(); } catch( MalformedURLException ex ) { return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy