3

I have a spring boot jar file and inside it a manifest file as below

Manifest-Version: 1.0
Implementation-Title: myApp
Implementation-Version: 0.1
Built-By: me
Implementation-Vendor-Id: com.myApp
Spring-Boot-Version: 2.0.0.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.myApp.smartlight.BootMongoDBApp
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.6.1
Build-Jdk: 1.8.0_151

And inside it is a class file named com.myApp.initiate.Initiator (packaged under BOOT-INF/classes folder inside the jar). I am trying to run Initiator class from command line on Windows machine as below

java -cp myApp.jar com.myApp.initiate.Initiator

but no luck. I also tried mentioning classpath in the above command as

java -cp "myApp.jar;BOOT-INF/*" com.myApp.initiate.Initiator

but it still doesn't work.

What am I doing wrong?

Initiator.java

package com.myApp.initiate.Initiator;

public class Initiator {

    public static void main(String... args) {

        System.out.print("hello");
    }
}

Update :

Initiator class was packaged under BOOT-INF/classes folder. When I copied it at the jar root and tried below command it worked

myApp.jar
|
|--org
|--BOOT-INF
|--META-INF
|--Initiator.class
java -cp myApp.jar Initiator
hello

Update: Thanks to @Strelok for the hints, I managed to get it working thru

java -cp myApp.jar -Dloader.main=com.myApp.initiate.Initiator org.springframework.boot.loader.PropertiesLauncher

qwerty
  • 2,392
  • 3
  • 30
  • 55
  • Can you add somepeace of code so we can see what your application do? – Jens Feb 25 '21 at 11:17
  • Why do you think that is *still doesn't work*? BTW use only lower case character in package names – Jens Feb 25 '21 at 11:18
  • @Jens It is just a regular Spring Boot application which runs fine through command line. Ans Initiator class just prints "hello" for now nothing more. I run the above mentioned commands from the same directory as the jar file. – qwerty Feb 25 '21 at 11:26
  • Where is it a spring-boot app. Where is the `@SpringBootApplication` annotation? – Jens Feb 25 '21 at 12:08

2 Answers2

4

Specify the class path, and loader.main, and the springboot launcher, it works as qwerty suggested:

java -cp myApp.jar -Dloader.main=com.myApp.initiate.Initiator org.springframework.boot.loader.PropertiesLauncher
david euler
  • 714
  • 8
  • 12
3

Standard way to run a Spring Boot app jar is with

java -jar myApp.jar

To run a different class you need to switch to using the org.springframework.boot.loader.PropertiesLauncher and add the loader.main property to you command line to choose a different class.

So you manifest must contain:

Main-Class: org.springframework.boot.loader.PropertiesLauncher

Then on the command line:

java -jar myApp.jar -Dloader.main=com.myApp.initiate.Initiator

Have a look at the docs as well for more information: https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-executable-jar-format.html#executable-jar-property-launcher-features

Strelok
  • 50,229
  • 9
  • 102
  • 115
  • The jar runs fine through the above command. I want to run a different class file (Initiator) before I run my Spring boot app. You may check the manifest entries mentioned in OP as what I am trying to run is not a main class/start class. – qwerty Feb 25 '21 at 11:25
  • @qwerty have a look at my updated answer. I missed the fact that your `Start-Class` was a different one in the manifest. – Strelok Feb 25 '21 at 11:34
  • Thanks for the update. ```java -jar``` will run the Spring Boot jar...which I don't want in my case. I am just trying to RUN a CLASS FILE which lies inside that jar at mentioned folder. – qwerty Feb 25 '21 at 11:40
  • Read carefully. You need to switch to PropertiesLauncher and specify an alternative class with `-Dloader.main` – Strelok Feb 25 '21 at 11:56
  • 4
    I managed to get it working thru : ```java -cp myApp.jar -Dloader.main=com.myApp.initiate.Initiator org.springframework.boot.loader.PropertiesLauncher``` – qwerty Feb 25 '21 at 12:33