Skip to content

Commit

Permalink
Remove the ClassLoaderPlus
Browse files Browse the repository at this point in the history
I do not know when we ever need it these days. Jaunch
(https://github.com/apposed/jaunch) and other smart native launchers
can take care of constructing the correct system classpath for us.

It was quite cool e.g. to be able to add entire folders of JARs to the
classpath via the `-jarpath` option. However, the custom class loading
logic has downsides, too:

* Starting in Java 9, the system class loader is not necessarily a
  URLClassLoader anymore, which causes ClassCastException whenever we
  assume it is and do a hard cast.

* The ClassLoaderPlus invokes Thread#setContextClassLoader() statically,
  meaning any time you start using it, the class loader for the current
  thread gets overwritten. This fact makes it hard to unit test, as
  evidenced by the failing test in the previous commit.

  I tried adding a static clear() method to ClassLoaderPlus that cleared
  out all the static data structures and reset the context class loader
  back to what it was before, but I was unable to make the test pass
  as part of the entire ClassLoaderPlusTest suite; only when run alone.

* The custom class loading logic is significantly more complex than
  allowing Java's system class loader to take care of class loading via
  the system class path. It is a separate code path from the other ways
  an application might get invoked, such as from within an Integrated
  Development Environment (IDE), meaning some class-loading-related
  issues might occur only when the application is loaded and launched
  via the ClassLoaderPlus mechanism, but not when launched from an IDE.

Therefore, to simplify the situation, I am paring back the scope of this
app-launcher component to avoid use of custom class loaders in favor of
only the default class loading mechanism.

Obviously, this breaks backwards compatibility, so we bump to 2.x.
  • Loading branch information
ctrueden committed Nov 11, 2024
1 parent 1a2c80d commit 25be3db
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 373 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>app-launcher</artifactId>
<version>1.2.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>

<name>SciJava App Launcher</name>
<description>Launcher for SciJava applications.</description>
Expand Down
39 changes: 4 additions & 35 deletions src/main/java/org/scijava/launcher/ClassLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

package org.scijava.launcher;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLClassLoader;
Expand All @@ -41,12 +40,12 @@
* Benefits:
* </p>
* <ol>
* <li>Call all Java main classes via this class, to be able to generate
* appropriate class paths using the platform-independent convenience provided
* by Java's class library.</li>
* <li>Verify that the version of Java being used is appropriate
* (e.g. new enough) for the application before attempting to launch it.</li>
* <li>Display a splash window while the application is starting up.</li>
* <li>Call all Java main classes via this class, to be able to invoke
* the above-mentioned pre-application-startup routines without
* application code needing to depend on the app-launcher directly.</li>
* </ol>
*
* @author Johannes Schindelin
Expand Down Expand Up @@ -78,30 +77,12 @@ private static void tryToRun(Runnable<?> r) {
}

private static void run(String[] args) {
boolean passClasspath = false;
URLClassLoader classLoader = null;
int i = 0;
for (; i < args.length && args[i].charAt(0) == '-'; i++) {
final String option = args[i];
switch (option) {
case "-cp":
case "-classpath":
classLoader = ClassLoaderPlus.get(classLoader, new File(args[++i]));
break;
case "-jarpath":
final String jarPaths = args[++i];
for (final String jarPath : jarPaths.split(File.pathSeparator)) {
final File jarDir = new File(jarPath);
if (!jarDir.exists()) continue;
classLoader = ClassLoaderPlus.getRecursively(classLoader, true, jarDir);
}
break;
case "-pass-classpath":
passClasspath = true;
break;
case "-freeze-classloader":
ClassLoaderPlus.freeze(classLoader);
break;
// Note: No options for now, but might add some in the future.
default:
Log.error("Unknown option: " + option + "!");
System.exit(1);
Expand All @@ -116,10 +97,6 @@ private static void run(String[] args) {
String mainClass = args[i];
args = slice(args, i + 1);

if (passClasspath && classLoader != null) {
args = prepend(args, "-classpath", ClassLoaderPlus.getClassPath(classLoader));
}

Log.debug("Launching main class " + mainClass + " with parameters " + Arrays.toString(args));

try {
Expand All @@ -143,14 +120,6 @@ private static String[] slice(final String[] array, final int from,
return result;
}

private static String[] prepend(final String[] array, final String... before) {
if (before.length == 0) return array;
final String[] result = new String[before.length + array.length];
System.arraycopy(before, 0, result, 0, before.length);
System.arraycopy(array, 0, result, before.length, array.length);
return result;
}

static void launch(ClassLoader classLoader,
final String className, final String... args)
{
Expand Down
164 changes: 0 additions & 164 deletions src/main/java/org/scijava/launcher/ClassLoaderPlus.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/org/scijava/launcher/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.io.*;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

Expand Down
96 changes: 0 additions & 96 deletions src/main/java/org/scijava/launcher/JarLauncher.java

This file was deleted.

Loading

0 comments on commit 25be3db

Please sign in to comment.