Topics

Spring Boot Choose Main Class #spring #java #springboot


Mrc0113
 

Hi, 

I have a Spring Boot project which has multiple main classes (Yes - I know not best practices, but I'm not able to easily change it). The project is being built by maven. 
Locally I was able to specify a "start-class" on the spring-boot-maven-plugin and that works to define which main I want to run, however when I push the app to PCF that doesn't seem to work. 
It looks like my app is using the java buildpack... Is there someway to specify the main class that I want to run in PCF?  Maybe by settings an environment variable or as part of the cf push command? 

thanks! 


Steve Taylor
 


Regards,
--Steve

On Tue, Apr 16, 2019 at 1:32 PM <marcd@...> wrote:
Hi, 

I have a Spring Boot project which has multiple main classes (Yes - I know not best practices, but I'm not able to easily change it). The project is being built by maven. 
Locally I was able to specify a "start-class" on the spring-boot-maven-plugin and that works to define which main I want to run, however when I push the app to PCF that doesn't seem to work. 
It looks like my app is using the java buildpack... Is there someway to specify the main class that I want to run in PCF?  Maybe by settings an environment variable or as part of the cf push command? 

thanks! 


Mrc0113
 

thanks Steve - at first I thought this would work, but it looks like Spring Boot is using "org.springframework.boot.loader.JarLauncher" as the main class so I don't want to override that. It looks like when run in an executable jar the JarLauncher looks at the Start-Class in the MANIFEST.MF file, but it doesn't seem to be respecting that in cf. 
I sshed into the cf container and found that the MANIFEST looks correct (it has the main-class set to JarLauncher and start-class set to my class), but it also seems that during the cf push the executable jar was exploded. It seems that after being exploded the JarLauncher class is no longer respecting the "Start-Class" set in the Manifest.  Any insight into that? 


Ben Hale
 

This is more a question for the Spring Boot team, but here's how Spring Boot applications in Cloud Foundry work.

When a Spring Boot application is created, your application is put into `/BOOT-INF` and a "wrapper" application is put into the root of the JAR. The `Main-Class` manifest entry is pointed at one of the Boot launchers (`JarLauncher`, `WarLauncher`, or `PropertiesLauncher`). Then when your application is started via `java -jar app.jar`, that `*Launcher` bootstraps a classpath with everything in `BOOT-INF` and then invokes the value read from the manifest `Start-Class` entry. So the only user of `Start-Class` is Boot itself, not the JVM.

In Cloud Foundry we are presented with the exploded JAR file, which makes no difference to how the application is invoked. We create a classpath at the root of the application and invoke the `Main-Class` entry of the manifest (`java -cp . <Main-Class>`) bootstrapping the Boot `*Launcher` as if it had been executed with `java -jar`. At this time, Boot takes over and creates its own internal classloader and invokes `Start-Class` as it would normally do.

If you are having an issue where Boot is not invoking the entry in `Start-Class` then that's a question for the Boot team, not Cloud Foundry.


-Ben Hale
Cloud Foundry Java Lead

On Apr 17, 2019, at 07:22, Mrc0113 <@Mrc0113> wrote:

thanks Steve - at first I thought this would work, but it looks like Spring Boot is using "org.springframework.boot.loader.JarLauncher" as the main class so I don't want to override that. It looks like when run in an executable jar the JarLauncher looks at the Start-Class in the MANIFEST.MF file, but it doesn't seem to be respecting that in cf.
I sshed into the cf container and found that the MANIFEST looks correct (it has the main-class set to JarLauncher and start-class set to my class), but it also seems that during the cf push the executable jar was exploded. It seems that after being exploded the JarLauncher class is no longer respecting the "Start-Class" set in the Manifest. Any insight into that?


Mrc0113
 

Ben, 

This definitely helps me understand what's going on - thanks for the detailed explanation. I'll continue to look into it...I started on the Cloud Foundry list because it was working fine when run outside of cloud foundry; it's only when I pushed it to cf that this seems to happen. 

-Marc