// Pick some things out of the top-level JAR file.
String jar = getMyJarPath();
JarFile jarFile = new JarFile(jar);
Manifest manifest = jarFile.getManifest();
Attributes attributes = manifest.getMainAttributes();
String bootLoaderName = attributes.getValue(ONE_JAR_CLASSLOADER);
if (mainJar == null) {
mainJar = attributes.getValue(ONE_JAR_DEFAULT_MAIN_JAR);
}
String mainargs = attributes.getValue(ONE_JAR_MAIN_ARGS);
if (mainargs != null && args.length == 0) {
// Replace the args with built-in. Support escaped whitespace.
args = mainargs.split("[^\\\\]\\s");
for (int i=0; i<args.length; i++) {
args[i] = args[i].replaceAll("\\\\(\\s)", "$1");
}
}
// If no main-class specified, check the manifest of the main jar for
// a Boot-Class attribute.
if (mainClass == null) {
mainClass = attributes.getValue(ONE_JAR_MAIN_CLASS);
if (mainClass == null) {
mainClass = attributes.getValue(BOOT_CLASS);
if (mainClass != null) {
LOGGER.warning("The manifest attribute " + BOOT_CLASS + " is deprecated in favor of the attribute " + ONE_JAR_MAIN_CLASS);
}
}
}
if (mainClass == null) {
// Still don't have one (default). One final try: look for a jar file in a
// main directory. There should be only one, and it's manifest
// Main-Class attribute is the main class. The JarClassLoader will take
// care of finding it.
InputStream is = Boot.class.getResourceAsStream("/" + mainJar);
if (is != null) {
JarInputStream jis = new JarInputStream(is);
Manifest mainmanifest = jis.getManifest();
jis.close();
mainClass = mainmanifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
} else {
// There is no main jar. Info unless mainJar is empty string.
// The load(mainClass) will scan for main jars anyway.
if (!"".equals(mainJar)){
LOGGER.info("Unable to locate main jar '" + mainJar + "' in the JAR file " + getMyJarPath());
}
}
}
// Do we need to create a wrapping classloader? Check for the
// presence of a "wrap" directory at the top of the jar file.
URL url = Boot.class.getResource(WRAP_JAR);
if (url != null) {
// Wrap class loaders.
final JarClassLoader bootLoader = getBootLoader(bootLoaderName);
bootLoader.load(null);
// Read the "Wrap-Class-Loader" property from the wraploader jar file.
// This is the class to use as a wrapping class-loader.
InputStream is = Boot.class.getResourceAsStream(WRAP_JAR);
if (is != null) {
JarInputStream jis = new JarInputStream(is);
final String wrapLoader = jis.getManifest().getMainAttributes().getValue(WRAP_CLASS_LOADER);
jis.close();
if (wrapLoader == null) {
LOGGER.warning(url + " did not contain a " + WRAP_CLASS_LOADER + " attribute, unable to load wrapping classloader");
} else {
LOGGER.info("using " + wrapLoader);
JarClassLoader wrapped = getWrapLoader(bootLoader, wrapLoader);
if (wrapped == null) {
LOGGER.warning("Unable to instantiate " + wrapLoader + " from " + WRAP_DIR + ": using default JarClassLoader");
wrapped = getBootLoader(null);
}
setClassLoader(wrapped);
}
}
} else {
setClassLoader(getBootLoader(bootLoaderName, Boot.class.getClassLoader()));
LOGGER.info("using JarClassLoader: " + getClassLoader().getClass().getName());
}
// Allow injection of the URL factory.
String urlfactory = attributes.getValue(ONE_JAR_URL_FACTORY);
if (urlfactory != null) {
loader.setURLFactory(urlfactory);
}
mainClass = loader.load(mainClass);