简体   繁体   中英

Build jar and run spring boot from cmd

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.
           }
}
  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

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

Follow these steps

for eclipse/spring tool suite

  • right click on project name to get these options --> Click on run as maven build 右键单击项目名称以获取这些选项

  • Add Goal as package and click on Run button at bottom right corner包装目标

  • jar file will be generated inside target folder of your project. You can locate this and then run java -jar command在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM