1

I am trying to identify a way to know the name of the Main-Class that started SpringBoot. e.g.

@SpringBootApplication
public class SampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }


@RestController
public class SampleController
{
   @GetMapping("/hello")
   pubic String sayHello()
   {
      System.out.println("Need start class name: "+System.getProperty("sun.java.command"));
      return "hello";
   }

}

}

When I run the springboot using java -jar myappname.jar ,the System.getProperty("sun.java.command") returns org.springframework.boot.loader.JarLauncher

Can anyone advise, how can I get the name of actual run class. I have tried specifying the start-class attribute in the manifest.mf. It still gave me org.springframework.boot.loader.JarLauncher as the start-class.

Jay
  • 71
  • 1
  • 1
  • 9
  • SampleApplication.class.getName()? What concrete problem are you trying to solve? – JB Nizet Dec 12 '18 at 17:14
  • I need to find the version of the running jar without necessarily looking in to Manifest.MF. In pure Java sense, I would like to use `Package pack = Package.getPackage(mainClassName); pack.getSpecificationVersion() ` That is why, I need mainClassName that has @SpringBootApplication annotated. – Jay Dec 12 '18 at 17:26
  • Why don't you include the version as a property in the application resources? Would be much easier. The spring boot maven/gradle plugins do that for you: https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/gradle-plugin/reference/html/#integrating-with-actuator-build-info, https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/maven-plugin/build-info-mojo.html – JB Nizet Dec 12 '18 at 17:36

1 Answers1

0

You should be able to @Autowire in the ApplicationContext and then do context.getBeansWithAnnotation(SpringBootApplication.class).values().toArray()[0].getClass().getName(), which will give you the first (and presumably only) bean in the context annotated with @SpringBootApplication

Ryan Dawson
  • 11,832
  • 5
  • 38
  • 61
  • Thanks for the reply Ryan. How do I get this name, if my Bean is not a Spring-Bean but a simple Java class ? Also, part of my code gets executed event before the spring banner is displayed. I do not have context at that time. – Jay Dec 12 '18 at 17:25
  • Perhaps we can chat on this - https://chat.stackoverflow.com/rooms/185157/room-for-ryan-dawson-and-jay – Ryan Dawson Dec 12 '18 at 17:27