Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I setup the nested class loading when launched by apache commons daemon jsvc?

I want to use jsvc to start my spring boot application because it's on the target system already and the alternative is to spend time debugging shell scripts for edge cases. I've implemented the Daemon interface so that SpringApplication.run() is called in Daemon.start() but the nested jars cannot be found because I've bypassed the JarLoader.

Is there a way to programatically set up the correct class loaders etc.?

@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableConfigurationProperties
public class Application implements Daemon {
 private ConfigurableApplicationContext ctx;
 private String[] args;

  @Override
  public void init(DaemonContext context) throws Exception {
    args = context.getArguments();
  }

  @Override
  public void start() throws Exception {
    ctx = SpringApplication.run(Application.class, args);
  }

  @Override
  public void stop() throws Exception {
    ctx.stop();
  }

  @Override
  public void destroy() {
    ctx.close();
  }

  // Main - mostly for development.
  public static void main(String[] args) throws Exception {
    System.err.println("WARNING - running as current user");
    DaemonLoader.Context ctx = new DaemonLoader.Context();
    Application app = new Application();
    ctx.setArguments(args);
    app.init(ctx);
    app.start();
  }
}

This errors with

java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
like image 345
Paul Thomas Avatar asked Sep 04 '14 11:09

Paul Thomas


1 Answers

I've solved this problem.

I had to produce shaded jar instead of fat jar. Then instead of using JarLoader, i've changed the main class directly to my main class. In case of original poster to class "Application".

like image 115
balki Avatar answered Nov 07 '22 22:11

balki