Skip to content
Snippets Groups Projects
Commit 96996da2 authored by Jaspal Singh Dhillon's avatar Jaspal Singh Dhillon Committed by Pekka Enberg
Browse files

java: Don't throw exceptions at user when the main class is not found

Fixes https://github.com/cloudius-systems/mgmt/issues/33



If a user runs 'java xyz', instead of throwing the stacktrace, a simple
message informing the user about the missing class, should suffice.

Signed-off-by: default avatarJaspal Singh Dhillon <jaspal.iiith@gmail.com>
Reviewed-by: default avatarTomasz Grabiec <tgrabiec@gmail.com>
Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>
parent d6dfa22d
No related branches found
No related tags found
No related merge requests found
......@@ -107,7 +107,9 @@ public class ContextIsolator {
runMain(loadClass(mainClass), args);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Throwable e) {
} catch (MainClassNotFoundException e) {
context.setException(e);
} catch (Throwable e) {
getUncaughtExceptionHandler().uncaughtException(this, e);
}
}
......@@ -253,8 +255,13 @@ public class ContextIsolator {
return new File(path).isDirectory();
}
private static Class<?> loadClass(String name) throws ClassNotFoundException {
return Thread.currentThread().getContextClassLoader().loadClass(name);
private static Class<?> loadClass(String name) throws MainClassNotFoundException {
try {
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(name);
return clazz;
} catch (ClassNotFoundException ex) {
throw new MainClassNotFoundException(name);
}
}
// Expand classpath, as given in the "-classpath" option, to a list of
......
package io.osv;
/*
* Copyright (C) 2014 Jaspal Singh Dhillon
*
* This work is open source software, licensed under the terms of the
* BSD license as described in the LICENSE file in the top-level directory.
*/
public class MainClassNotFoundException extends Exception {
private String mainClassName;
public MainClassNotFoundException(String mainClass) {
mainClassName = mainClass;
}
public String getClassName() {
return mainClassName;
}
}
......@@ -26,7 +26,13 @@ public class RunJava {
ContextIsolator.getInstance().runSync(args);
} catch (IllegalArgumentException ex) {
System.err.println("RunJava: " + ex.getMessage());
} catch (Throwable ex) {
} catch (ContextFailedException ex) {
if (ex.getCause() instanceof MainClassNotFoundException) {
System.err.println("Error: Could not find or load main class " + ((MainClassNotFoundException)ex.getCause()).getClassName());
} else {
ex.printStackTrace();
}
} catch (Throwable ex) {
ex.printStackTrace();
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment