in src/main/org/apache/tools/ant/launch/Launcher.java [173:308]
private int run(final String[] args)
throws LaunchException, MalformedURLException {
final String antHomeProperty = System.getProperty(ANTHOME_PROPERTY);
File antHome = null;
final File sourceJar = Locator.getClassSource(getClass());
final File jarDir = sourceJar.getParentFile();
String mainClassname = MAIN_CLASS;
if (antHomeProperty != null) {
antHome = new File(antHomeProperty);
}
if (antHome == null || !antHome.exists()) {
antHome = jarDir.getParentFile();
setProperty(ANTHOME_PROPERTY, antHome.getAbsolutePath());
}
if (!antHome.exists()) {
throw new LaunchException(
"Ant home is set incorrectly or ant could not be located (estimated value="
+ antHome.getAbsolutePath() + ")");
}
final List<String> libPaths = new ArrayList<>();
String cpString = null;
final List<String> argList = new ArrayList<>();
String[] newArgs;
boolean noUserLib = false;
boolean noClassPath = false;
for (int i = 0; i < args.length; ++i) {
if ("-lib".equals(args[i])) {
if (i == args.length - 1) {
throw new LaunchException(
"The -lib argument must be followed by a library location");
}
libPaths.add(args[++i]);
} else if ("-cp".equals(args[i])) {
if (i == args.length - 1) {
throw new LaunchException(
"The -cp argument must be followed by a classpath expression");
}
if (cpString != null) {
throw new LaunchException(
"The -cp argument must not be repeated");
}
cpString = args[++i];
} else if ("--nouserlib".equals(args[i]) || "-nouserlib".equals(args[i])) {
noUserLib = true;
} else if ("--launchdiag".equals(args[i])) {
launchDiag = true;
} else if ("--noclasspath".equals(args[i]) || "-noclasspath".equals(args[i])) {
noClassPath = true;
} else if ("-main".equals(args[i])) {
if (i == args.length - 1) {
throw new LaunchException(
"The -main argument must be followed by a library location");
}
mainClassname = args[++i];
} else {
argList.add(args[i]);
}
}
logPath("Launcher JAR", sourceJar);
logPath("Launcher JAR directory", sourceJar.getParentFile());
logPath("java.home", new File(System.getProperty("java.home")));
//decide whether to copy the existing arg set, or
//build a new one from the list of all args excluding the special
//operations that only we handle
if (argList.size() == args.length) {
newArgs = args;
} else {
newArgs = argList.toArray(new String[0]);
}
final URL[] libURLs = getLibPathURLs(
noClassPath ? null : cpString, libPaths);
final URL[] systemURLs = getSystemURLs(jarDir);
final URL[] userURLs = noUserLib ? new URL[0] : getUserURLs();
final File toolsJAR = Locator.getToolsJar();
logPath("tools.jar", toolsJAR);
final URL[] jars = getJarArray(
libURLs, userURLs, systemURLs, toolsJAR);
// now update the class.path property
final StringBuilder baseClassPath
= new StringBuilder(System.getProperty(JAVA_CLASS_PATH));
if (baseClassPath.charAt(baseClassPath.length() - 1)
== File.pathSeparatorChar) {
baseClassPath.setLength(baseClassPath.length() - 1);
}
for (URL jar : jars) {
baseClassPath.append(File.pathSeparatorChar);
baseClassPath.append(Locator.fromURI(jar.toString()));
}
setProperty(JAVA_CLASS_PATH, baseClassPath.toString());
final URLClassLoader loader = new URLClassLoader(jars, Launcher.class.getClassLoader());
Thread.currentThread().setContextClassLoader(loader);
Class<? extends AntMain> mainClass = null;
int exitCode = 0;
Throwable thrown = null;
try {
mainClass = loader.loadClass(mainClassname).asSubclass(AntMain.class);
final AntMain main = mainClass.getDeclaredConstructor().newInstance();
main.startAnt(newArgs, null, null);
} catch (final InstantiationException ex) {
System.err.println(
"Incompatible version of " + mainClassname + " detected");
final File mainJar = Locator.getClassSource(mainClass);
System.err.println(
"Location of this class " + mainJar);
thrown = ex;
} catch (final ClassNotFoundException cnfe) {
System.err.println(
"Failed to locate" + mainClassname);
thrown = cnfe;
} catch (final Throwable t) {
t.printStackTrace(System.err); //NOSONAR
thrown = t;
}
if (thrown != null) {
System.err.println(ANTHOME_PROPERTY + ": " + antHome.getAbsolutePath());
System.err.println("Classpath: " + baseClassPath.toString());
System.err.println("Launcher JAR: " + sourceJar.getAbsolutePath());
System.err.println("Launcher Directory: " + jarDir.getAbsolutePath());
exitCode = EXIT_CODE_ERROR;
}
return exitCode;
}