Java 如何通过命令行启动 spring-boot 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47835901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:41:37  来源:igfitidea点击:

How to start up spring-boot application via command line?

javaspring-boot

提问by user4900074

I have a spring-boot application which I need to start up by going to the folder directory and start up my web application via command line. I have a class called Application.java and the code inside it is as followed.

我有一个 spring-boot 应用程序,我需要通过转到文件夹目录来启动它,并通过命令行启动我的 Web 应用程序。我有一个名为 Application.java 的类,其中的代码如下。

@SpringBootApplication(scanBasePackages = {"com.ubs.tas.topcat.dashboard"})
public class Application extends SpringBootServletInitializer {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class.getName());
    private static final Class<Application> applicationClass = Application.class;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    public static void main(String[] args) {
        LOGGER.info("Starting...");
        SpringApplication.run(Application.class, args);
    }
}

I set up classpath then tried to run the command "java ApplicationUtility" but I'm getting this error message "Could not find the main class: ApplicationUtility. Program will exist."

我设置了类路径,然后尝试运行命令“ java ApplicationUtility”,但我收到此错误消息“ Could not find the main class: ApplicationUtility. Program will exist.

采纳答案by Srikanth Anusuri

You will need to build the jar file first. Here is the syntax to run the main class from a jar file.

您需要先构建 jar 文件。这是从 jar 文件运行主类的语法。

java -jar path/to/your/jarfile.jar fully.qualified.package.Application 

回答by Adindu Stevens

I presume you are trying to compile the application and run it without using an IDE. I also presume you have maven installed and correctly added maven to your environment variable.

我假设您正在尝试编译应用程序并在不使用 IDE 的情况下运行它。我还假设您已经安装了 maven 并将 maven 正确添加到您的环境变量中。

To install and add maven to environment variable visit Install mavenif you are under a proxy check out add proxy to maven

要安装 maven 并将其添加到环境变量,请访问Install maven如果您在代理下,请查看add proxy to maven

Navigate to the root of the project via command line and execute the command

通过命令行导航到项目的根目录并执行命令

mvn spring-boot:run

The CLI will run your application on the configured port and you can access it just like you would if you start the app in an IDE.

CLI 将在配置的端口上运行您的应用程序,您可以像在 IDE 中启动应用程序一样访问它。

Note: This will work only if you have maven added to your pom.xml

注意:这仅在您将 maven 添加到 pom.xml 时才有效

回答by soyphea

Spring Boot provide the plugin with maven.

Spring Boot 为插件提供了 maven。

So you can go to your project directory and run

所以你可以去你的项目目录并运行

mvn spring-boot:run

This command line run will be easily when you're using spring-boot-devs-tool with auto reload/restart when you have changed you application.

当您使用 spring-boot-devs-tool 并在更改应用程序后自动重新加载/重新启动时,此命令行运行将很容易。

回答by Abdur Rahman

To run the spring-boot application, need to follow some step.

要运行 spring-boot 应用程序,需要执行一些步骤。

  1. Maven setup (ignore if already setup):

    a. Install maven from https://maven.apache.org/download.cgi

    b. Unzip maven and keep in C drive (you can keep any location. Path location will be changed accordingly).

    c. Set MAVEN_HOME in system variable. enter image description here

    d. Set path for maven

  1. Maven 设置(如果已经设置,请忽略):

    一种。从https://maven.apache.org/download.cgi安装 maven

    湾 解压 maven 并保存在 C 盘(您可以保存任何位置。路径位置将相应更改)。

    C。在系统变量中设置 MAVEN_HOME。 在此处输入图片说明

    d. 为maven设置路径

enter image description here

在此处输入图片说明

  1. Add Maven Plugin to POM.XML

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

  2. Build Spring Boot Project with Maven

       maven package
    

    or

         mvn install / mvn clean install
    
  3. Run Spring Boot app using Maven:

        mvn spring-boot:run
    
  4. [optional]Run Spring Boot app with java -jar command

         java -jar target/mywebserviceapp-0.0.1-SNAPSHOT.jar
    
  1. 将 Maven 插件添加到 POM.XML

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

  2. 使用 Maven 构建 Spring Boot 项目

       maven package
    

    或者

         mvn install / mvn clean install
    
  3. 使用 Maven 运行 Spring Boot 应用程序:

        mvn spring-boot:run
    
  4. [可选]使用 java -jar 命令运行 Spring Boot 应用程序

         java -jar target/mywebserviceapp-0.0.1-SNAPSHOT.jar
    

回答by Paula T

If you're using gradle, you can use:

如果您使用的是 gradle,则可以使用:

./gradlew bootRun

回答by Badri Paudel

One of the ways that you can run your spring-boot application from command line is as follows :

您可以从命令行运行 spring-boot 应用程序的方法之一如下:

1) First go to your project directoryin command line [where is your project located ?]

1)首先在命令行中转到您的项目目录[您的项目位于何处?]

2) Then in the next step you have to create jar filefor that, this can be done as

2) 然后在下一步中,您必须为此创建 jar 文件,这可以这样做

mvnw package[for WINDOWS OS] or ./mvnw package[for MAC OS] , this will create jar filefor our application.

mvnw package[对于 WINDOWS 操作系统] 或./mvnw package[对于 MAC 操作系统],这将为我们的应用程序创建 jar 文件

3) jar file is created in the targetsub-directory

3)在目标子目录下创建jar文件

4)Now go to target sub directoryas jar was created inside of it , i.e cd target

4)现在转到目标子目录,因为在其中创建了jar,即cd target

5) Now run the jar file in there. Use command java -jar name.jar[ name is the name of your created jar file.]

5) 现在在那里运行 jar 文件。使用命令java -jar name.jar[name 是您创建的 jar 文件的名称。]

and there you go , you are done . Now you can run project in browser,

好了,大功告成。现在您可以在浏览器中运行项目,

 http://localhost:port_number

回答by Hariharan

A Spring Boot project configured through Maven can be run using the following command from the project source folder

通过 Maven 配置的 Spring Boot 项目可以在项目源文件夹中使用以下命令运行

mvn spring-boot:run

回答by Neeraj Gahlawat

1.Run Spring Boot app with java -jar command

1.使用 java -jar 命令运行 Spring Boot 应用程序

To run your Spring Boot app from a command line in a Terminal window you can use java -jar command. This is provided your Spring Boot app was packaged as an executable jar file.

要从终端窗口中的命令行运行 Spring Boot 应用程序,您可以使用 java -jar 命令。前提是您的 Spring Boot 应用程序被打包为一个可执行的 jar 文件。

java -jar target/app-0.0.1-SNAPSHOT.jar

2.Run Spring Boot app using Maven

2. 使用 Maven 运行 Spring Boot 应用程序

You can also use Maven plugin to run your Spring Boot app. Use the below command to run your Spring Boot app with Maven plugin:

您还可以使用 Maven 插件来运行您的 Spring Boot 应用程序。使用以下命令通过 Maven 插件运行您的 Spring Boot 应用程序:

mvn spring-boot:run

3.Run Spring Boot App with Gradle

3.使用 Gradle 运行 Spring Boot 应用程序

And if you use Gradle you can run the Spring Boot app with the following command:

如果您使用 Gradle,则可以使用以下命令运行 Spring Boot 应用程序:

gradle bootRun