Build jar and run spring boot from cmd
Asked Answered
M

4

10

I'm new to springboot and using springboot 2 version.

I would like to run my spring boot application using : java -jar my-app-0.0.1-SNAPSHOT.jar from the command prompt.

However, when i build the application using eclipse it calls myService.getMyMethod() directly and does not build the jar.

I want to build the jar file first and then run java -jar my-app-0.0.1-SNAPSHOT.jar from the command prompt which will invoke myService.getMyMethod()

I already have spring-boot-maven-plugin in pom.xml, running mvn package / mvn install starts the application but not generating the jar file in the target folder. mvn clean package also not working, MyApplication is using implements CommandLineRunner and starts invoking the method on build, hence not generating the build jar file

My main class:

@SpringBootApplication
public class MyApplication implements CommandLineRunner {

private static String URL = "ws://localhost:8080/spring-mvc-java/chat";

   @Autowired
   private MyService myService;

   public static void main(String[] args) {
       SpringApplication.run(MyApplication.class, args);
   }
   @Override
   public void run(String... args) throws Exception {
       myService.getMyMethod(URL);
   }
}

My service class:

@Service
public class MyServiceImpl implements MyService {

     public void getMyMethod(String URL){
           WebSocketClient client = new StandardWebSocketClient();
           WebSocketStompClient stompClient = new WebSocketStompClient(client);
           stompClient.setMessageConverter(new MappingJackson2MessageConverter());
           StompSessionHandler sessionHandler = new MyStompSessionHandler();
           stompClient.connect(URL, sessionHandler);
           new Scanner(System.in).nextLine(); // Don't close immediately.
           }
}
Mariande answered 30/6, 2020 at 5:18 Comment(6)
Did you add the spring-boot-maven-plugin to your pom.xml to create the correct jar?Purposeless
Yes. Updated the postMariande
if mvn package doesn't generate the jar, but starts the app, you probably have a test that blocks the proces.Purposeless
@M.Deinum Please find the code updated aboveMariande
Your service is blocking, hence preventing the test from finishing and thus maven cannot produce a jar.Purposeless
paulsofts.com/how-to-package-spring-boot-application-to-jarBillionaire
R
7

In order to run the jar with java -jar you should use spring-boot-maven-plugin:

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.0.RELEASE</version>
            </plugin>
        </plugins>
    </build>

This plugin will "bake" all the dependencies of the project into the JAR and add some "technical" code that will allow to run this JAR with java -jar (the exact details are kind of out of scope for this question)

If you add it into the build, and run mvn package / mvn install it will create in the target folder a big JAR next to the original jar (that will be renamed with suffix .original).

You can open it with WinRAR/WinZIP and make sure that all your classes are there and all the dependencies are in BOOT-INF/lib folder.

Then you can run the project with java -jar

Resentment answered 30/6, 2020 at 6:3 Comment(4)
I have the spring-boot-maven-plugin in pom.xml but jar not generated. Updated my post.Mariande
run mvn clean package - by default the "repackage" goal is bound to "package" phase in maven (docs.spring.io/spring-boot/docs/1.5.2.RELEASE/maven-plugin/…)Resentment
mvn clean package also not working, my main class is using implements CommandLineRunner and starts invoking the method on buildMariande
try to generate the same project from start.spring.io and see the differences with your pom...Resentment
U
2
  1. In pom.xml add tag <packaging>jar</packaging>.
  2. you can build the maven project using command mvn package or maven install. It will create the .jar file inside target folder.
  3. run command from location where .jar is present - java -jar jarName.jar

Now you can access your system

Unloosen answered 28/4, 2021 at 11:4 Comment(0)
D
2

Follow these steps

for eclipse/spring tool suite

  • right click on project name to get these options --> Click on run as maven build right click on project name to get these options

  • Add Goal as package and click on Run button at bottom right corner goal for package

  • jar file will be generated inside target folder of your project. You can locate this and then run java -jar command enter image description here

Drye answered 25/3, 2022 at 9:39 Comment(4)
Hijacking an older question with an answer that won't solve a thing isn't the way forward. The test is still blocking running mvn package through the UI won't fix that.Purposeless
It works, because when we do mvn build it generates jar for project. I am not getting what do you want to say exactly.Drye
They have a test that is reading input from the command line. The mvn package never finish. They already know they need the mvn package reiterating that doesn't make it suddenly work (they also mention the exact same in the question that mvn package never finishes).Purposeless
thanks for comment, I got your answer.Drye
C
0

if you don't use tests . you can use this solution . before you must delete test folder after you must run mvn clean install command in your project folder . after you can add this code in pom.xml anywhere <packaging>jar</packaging>and run mvn package command . the jar file will create .

Callida answered 11/4, 2023 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.