org.springframework.boot.loader.JarLauncher Java Examples

The following examples show how to use org.springframework.boot.loader.JarLauncher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExecJarRule.java    From pivotal-bank-demo with Apache License 2.0 4 votes vote down vote up
public ExecJarRule() {
  this.execJar = JarLauncher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
}
 
Example #2
Source File: ApplicationRunner.java    From liiklus with MIT License 4 votes vote down vote up
@SneakyThrows
public ConfigurableApplicationContext run() {
    System.setProperty("plugins.dir", findPluginsDir().getAbsolutePath());
    System.setProperty("plugins.pathMatcher", "*/build/libs/*.jar");

    var tempFile = Files.createTempFile("app", ".jar");
    tempFile.toFile().deleteOnExit();
    try (var appJarStream = getClass().getClassLoader().getResourceAsStream("app-boot.jar")) {
        Files.copy(appJarStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
    }

    var launcher = new JarLauncher(new JarFileArchive(tempFile.toFile(), tempFile.toUri().toURL())) {

        ClassLoader createClassLoader() throws Exception {
            return super.createClassLoader(getClassPathArchives());
        }

        @Override
        protected ClassLoader createClassLoader(URL[] urls) throws Exception {
            var systemClassLoader = ClassLoader.getSystemClassLoader();
            return new LaunchedURLClassLoader(urls, systemClassLoader.getParent()) {

                @Override
                protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
                    var classFile = findResource(name.replace(".", "/") + ".class");
                    if (classFile != null) {
                        // If exists in the app.jar, load it from the system classloader instead
                        log.debug("Loading class '{}' from the system ClassLoader instead", name);
                        return systemClassLoader.loadClass(name);
                    }
                    return super.loadClass(name, resolve);
                }
            };
        }
    };

    var currentClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        var appClassLoader = launcher.createClassLoader();
        Thread.currentThread().setContextClassLoader(appClassLoader);

        var applicationClass = appClassLoader.loadClass("com.github.bsideup.liiklus.Application");

        var createSpringApplicationMethod = applicationClass.getDeclaredMethod("createSpringApplication", String[].class);

        var application = (SpringApplication) createSpringApplicationMethod.invoke(null, (Object) new String[0]);
        application.setDefaultProperties(properties);
        return application.run();
    } finally {
        Thread.currentThread().setContextClassLoader(currentClassLoader);
    }
}