public JarLoader(Path jarPath, String entrypoint) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, SecurityException { super(new URL[] { jarPath.toUri().toURL() }); final String[] splittedEntrypoint = entrypoint.split("#"); final String entrypointClassName = splittedEntrypoint[0]; final String entrypointMethodName = splittedEntrypoint.length > 1 ? splittedEntrypoint[1] : "main"; this.mainClass = loadClass(entrypointClassName); Method m = mainClass.getMethod(entrypointMethodName, new Class[] { JsonObject.class }); m.setAccessible(true); int modifiers = m.getModifiers(); if (m.getReturnType() != JsonObject.class || !Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) { throw new NoSuchMethodException("main"); } this.mainMethod = m; }
public JsonObject launch() throws Exception { Invoker invoker; ClassLoader loader; if (JAVA_FILE_MATCHER.matches(this.binaryPath)) { loader = new JavaCompilerLoader(Thread.currentThread().getContextClassLoader(), this.getBinaryPath()); invoker = (Invoker) loader; } else { if (getEntryClassName() == null) { throw new IllegalStateException("Main class name is required to execute .jar"); } loader = new JarLoader(this.getBinaryPath(), getEntryClassName()); invoker = (Invoker) loader; } Thread.currentThread().setContextClassLoader(loader); JsonObject args = getParameter(); if (args == null) { args = new JsonObject(); //always pass an argument } return invoker.invokeMain(args); }