- java.lang.Object
-
- org.scijava.common3.Apps
-
public final class Apps extends Object
Useful methods for obtaining details of the SciJava application environment.- Author:
- Johannes Schindelin, Curtis Rueden
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FilebaseDirectory(File classLocation, String baseSubdirectory)Gets the base file system directory from the given known class location.static FilebaseDirectory(Class<?> c)Gets the base file system directory containing the given class file.static FilebaseDirectory(Class<?> c, String baseSubdirectory)Gets the base file system directory containing the given class file.static FilebaseDirectory(String sysProp, Class<?> c, String baseSubdirectory)Gets the application root directory.static Class<?>mainClass()Gets the class whose main method launched the application.
-
-
-
Method Detail
-
mainClass
public static Class<?> mainClass()
Gets the class whose main method launched the application.- Returns:
- The launching class, or null if the main method terminated before
the
AppUtilsclass was loaded.
-
baseDirectory
public static File baseDirectory(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:
baseDirectory(File, String)
-
baseDirectory
public static File baseDirectory(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:
baseDirectory(File, String)
-
baseDirectory
public static File baseDirectory(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:
baseDirectory(File, String)
-
baseDirectory
public static File baseDirectory(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:
- In the Maven build directory. Typically this is the
target/classesfolder 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 outscijava-common3.gitto~/sjc, theorg.scijava.Contextclass will be located at~/sjc/scijava-common3/target/classes/org/scijava/Context.class. Asking for the base directory for that class will yield~/sjc/scijava-common3, as long as you correctly specifyscijava-common3for thebaseSubdirectory. - Within a JAR file in the Maven local repository cache. Typically
this cache is located in
~/.m2/repository. The location will begroupId/artifactId/version/artifactId-version.jarwheregroupId,artifactIdandversionare 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. - 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
jarsfolder of the application distribution archive) or using a JAR packaged by Maven and residing in the Maven build folder (typicallytarget/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:
- 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.
- 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. - Running as an application bundle (e.g., Fiji). Typically this
means downloading Fiji from its web site, unpacking it and running it using
its native launcher. In this case, all components reside in the
jarsfolder 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.
- In the Maven build directory. Typically this is the
-
-