org.scijava.util
Class AppUtils

java.lang.Object
  extended by org.scijava.util.AppUtils

public final class AppUtils
extends Object

Useful methods for obtaining details of the SciJava application environment.

Author:
Johannes Schindelin, Curtis Rueden

Method Summary
static File getBaseDirectory(Class<?> c)
          Gets the base file system directory containing the given class file.
static File getBaseDirectory(Class<?> c, String baseSubdirectory)
          Gets the base file system directory containing the given class file.
static File getBaseDirectory(File classLocation, String baseSubdirectory)
          Gets the base file system directory from the given known class location.
static File getBaseDirectory(String sysProp, Class<?> c, String baseSubdirectory)
          Gets the application root directory.
static Class<?> getMainClass()
          Gets the class whose main method launched the application.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getMainClass

public static Class<?> getMainClass()
Gets the class whose main method launched the application.

Returns:
The launching class, or null if the main method terminated before the AppUtils class was loaded.

getBaseDirectory

public static File getBaseDirectory(String sysProp,
                                    Class<?> c,
                                    String baseSubdirectory)
Gets the application root directory. If the given system property is set, it is used. Otherwise, we scan up the tree from the given class for a suitable directory.

Parameters:
sysProp - System property which may point at the root directory. If this is set to a valid directory, it is used.
c - The class from which the base directory should be derived.
baseSubdirectory - A hint for what to expect for a directory structure beneath the application base directory. If this value is null (i.e., no hint is given), the heuristic scans up the directory tree looking for the topmost pom.xml file.
See Also:
getBaseDirectory(File, String)

getBaseDirectory

public static File getBaseDirectory(Class<?> c)
Gets the base file system directory containing the given class file.

Parameters:
c - The class from which the base directory should be derived.
See Also:
getBaseDirectory(File, String)

getBaseDirectory

public static File getBaseDirectory(Class<?> c,
                                    String baseSubdirectory)
Gets the base file system directory containing the given class file.

Parameters:
c - The class from which the base directory should be derived.
baseSubdirectory - A hint for what to expect for a directory structure beneath the application base directory.
See Also:
getBaseDirectory(File, String)

getBaseDirectory

public static File getBaseDirectory(File classLocation,
                                    String baseSubdirectory)
Gets the base file system directory from the given known class location.

This method uses heuristics to find the base directory in a variety of situations. Depending on your execution environment, the class may be located in one of several different places:

  1. In the Maven build directory. Typically this is the target/classes folder of a given component. In this case, the class files reside directly on the file system (not in a JAR file). The base directory is defined as the toplevel Maven directory for the multi-module project. For example, if you have checked out scijava-common.git to ~/sjc, the org.scijava.Context class will be located at ~/sjc/scijava-common/target/classes/org/scijava/Context.class. Asking for the base directory for that class will yield ~/sjc/scijava-common, as long as you correctly specify scijava-common for the baseSubdirectory.
  2. Within a JAR file in the Maven local repository cache. Typically this cache is located in ~/.m2/repository. The location will be groupId/artifactId/version/artifactId-version.jar where groupId, artifactId and version are the Maven GAV coordinates. Note that in this case, no base directory with respect to the given class can be found, and this method will return null.
  3. Within a JAR file beneath the base directory. Common cases include running the application from a standalone application bundle (e.g., the JARs may all be located within a jars folder of the application distribution archive) or using a JAR packaged by Maven and residing in the Maven build folder (typically target/artifactId-version.jar). However, this could potentially be anywhere beneath the base directory. This method assumes the JAR will be nested exactly one level deep; i.e., it computes the base directory as the parent directory of the one containing the JAR file.

As you can see, it is quite complicated to find the base directory properly in all cases. The specific execution environments we have tested include:

  1. Running from Eclipse. How Eclipse structures the classpath depends on which dependencies are open (i.e., project dependencies) versus closed (i.e., JAR dependencies). Each project dependency has a classpath entry in its Maven build directory. Each JAR dependency has a classpath entry within a JAR file in the Maven local repository cache.
  2. Running via Maven. If you execute the application using Maven (e.g., mvn exec:exec, or with a fully Maven-compatible IDE such as NetBeans or IntelliJ IDEA) then all dependencies will reside within JAR files in the local Maven repository cache. But the executed project itself will reside in its Maven build directory. So as long as you ask for the base directory relative to a class of the executed project it will be found.
  3. Running as an application bundle (e.g., ImageJ). Typically this means downloading ImageJ from the web site, unpacking it and running the ImageJ launcher (double-clicking ImageJ-win32.exe on Windows, double-clicking the ImageJ.app on OS X, etc.). In this case, all components reside in the jars folder of the application bundle, and the base directory will be found one level above that.

Parameters:
classLocation - The location from which the base directory should be derived.
baseSubdirectory - A hint for what to expect for a directory structure beneath the application base directory. If this value is null (i.e., no hint is given), the heuristic scans up the directory tree looking for the topmost pom.xml file.


Copyright © 2009–2015 SciJava. All rights reserved.