Java Code Examples for org.springframework.boot.loader.JarLauncher

The following examples show how to use org.springframework.boot.loader.JarLauncher. These examples are extracted from open source projects. 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 Project: pivotal-bank-demo   Source File:    License: Apache License 2.0 4 votes vote down vote up
public ExecJarRule() {
  this.execJar = JarLauncher.class.getProtectionDomain().getCodeSource().getLocation().getFile();
Example 2
Source Project: liiklus   Source File:    License: MIT License 4 votes vote down vote up
public ConfigurableApplicationContext run() {
    System.setProperty("plugins.dir", findPluginsDir().getAbsolutePath());
    System.setProperty("plugins.pathMatcher", "*/build/libs/*.jar");

    var tempFile = Files.createTempFile("app", ".jar");
    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());

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

                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();

        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]);
    } finally {