Skip to content

Commit

Permalink
ClassLauncher: indicate source of debug/error msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
hinerm committed Nov 20, 2024
1 parent b7cb938 commit 26de23c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/main/java/org/scijava/launcher/ClassLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ private static void run(String[] args) {
switch (option) {
// Note: No options for now, but might add some in the future.
default:
Log.error("Unknown option: " + option + "!");
error("Unknown option: " + option + "!");
System.exit(1);
}
}

if (i >= args.length) {
Log.error("Missing argument: main class");
error("Missing argument: main class");
System.exit(1);
}

String mainClass = args[i];
args = slice(args, i + 1);

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

try {
launch(classLoader, mainClass, args);
Expand All @@ -120,11 +120,19 @@ private static String[] slice(final String[] array, final int from,
return result;
}

private static void debug(String message) {
Log.debug("[ClassLauncher] " + message);
}

private static void error(String message) {
Log.error("[ClassLauncher] " + message);
}

static void launch(ClassLoader classLoader,
final String className, final String... args)
{
Class<?> main = null;
Log.debug("Class loader = " + classLoader);
debug("Class loader = " + classLoader);
try {
main = ClassLoaders.loadClass(classLoader, className);
}
Expand All @@ -145,7 +153,7 @@ static void launch(ClassLoader classLoader,
}
catch (final NoSuchMethodException e) {
Log.debug(e);
Log.error("Class '" + className + "' does not have a main method.");
error("Class '" + className + "' does not have a main method.");
System.exit(1);
}
Integer result = 1;
Expand All @@ -154,10 +162,10 @@ static void launch(ClassLoader classLoader,
}
catch (final IllegalAccessException e) {
Log.debug(e);
Log.error("The main method of class '" + className + "' is not public.");
error("The main method of class '" + className + "' is not public.");
}
catch (final InvocationTargetException e) {
Log.error("Error while executing the main method of class '" + className + "':");
error("Error while executing the main method of class '" + className + "':");
Log.error(e.getTargetException());
}
if (result != null) System.exit(result);
Expand Down

0 comments on commit 26de23c

Please sign in to comment.