public final class AppUtils extends Object
Modifier and Type | Method and Description |
---|---|
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.
|
public static Class<?> getMainClass()
AppUtils
class was loaded.public static File getBaseDirectory(String sysProp, Class<?> c, String baseSubdirectory)
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.getBaseDirectory(File, String)
public static File getBaseDirectory(Class<?> c)
c
- The class from which the base directory should be derived.getBaseDirectory(File, String)
public static File getBaseDirectory(Class<?> c, String baseSubdirectory)
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.getBaseDirectory(File, String)
public static File getBaseDirectory(File classLocation, String baseSubdirectory)
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:
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
.~/.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.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:
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.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.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 © 2015–2022 SciJava. All rights reserved.