# 6.7. 运行您的应用程序

将应用程序打包为 jar 并使用嵌入式 HTTP 服务器的最大优点之一是您可以像运行任何其他应用程序一样运行您的应用程序。该示例适用于调试Spring Boot应用程序。您不需要任何特殊的 IDE 插件或扩展。

> 本节仅涵盖基于 jar 的包装。如果您选择将应用程序打包为 war 文件，请参阅您的服务器和 IDE 文档。

**6.7.1. 从 IDE 运行**

您可以从 IDE 将 Spring Boot 应用程序作为 Java 应用程序运行。但是，您首先需要导入您的项目。导入步骤因您的 IDE 和构建系统而异。大多数IDE都可以直接导入Maven项目。例如，Eclipse 用户可以从`File`菜单中选择`Import…`→ `Existing Maven Projects`。

如果您无法将项目直接导入 IDE，则可以使用构建插件生成 IDE 元数据。[Maven 包括Eclipse](https://maven.apache.org/plugins/maven-eclipse-plugin/)和[IDEA](https://maven.apache.org/plugins/maven-idea-plugin/)的插件。[Gradle 为各种 IDE](https://docs.gradle.org/current/userguide/userguide.html)提供插件。

> 如果您不小心运行 Web 应用程序两次，您会看到“端口已在使用中”错误。Spring Tools 用户可以使用`Relaunch`按钮而不是`Run`按钮来确保关闭任何现有实例。

**6.7.2. 作为打包应用程序运行**

如果您使用 Spring Boot Maven 或 Gradle 插件创建可执行 jar，则可以使用`java -jar` 运行应用程序，如以下示例所示：

```
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
```

还可以运行启用了远程调试支持的打包应用程序。这样做可以让您将调试器附加到打包的应用程序中，如以下示例所示：

```
$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n \
       -jar target/myapplication-0.0.1-SNAPSHOT.jar
```

**6.7.3. 使用 Maven 插件**

Spring Boot Maven 插件包含一个可用于快速编译和运行应用程序的`run`目标。应用程序以分解形式运行，就像在 IDE 中一样。以下示例显示了运行 Spring Boot 应用程序的典型 Maven 命令：

```
$ mvn spring-boot:run
```

您可能还想使用`MAVEN_OPTS`操作系统环境变量，如以下示例所示：

```
$ export MAVEN_OPTS=-Xmx1024m
```

**6.7.4. 使用 Gradle 插件**

Spring Boot Gradle 插件还包含一个可用于以分解形式运行应用程序的`bootRun`任务。每当您应用`org.springframework.boot`和`java`插件时都会添加`bootRun`任务，如以下示例所示：

```
$ gradle bootRun
```

您可能还想使用`JAVA_OPTS`操作系统环境变量，如以下示例所示：

```
$导出JAVA_OPTS=-Xmx1024m
```

**6.7.5. 热插拔**

由于 Spring Boot 应用程序是普通的 Java 应用程序，因此 JVM 热插拔应该可以开箱即用。JVM 热交换在某种程度上受到它可以替换的字节码的限制。对于更完整的解决方案，可以使用[JRebel 。](https://www.jrebel.com/products/jrebel)

`spring-boot-devtools`模块还包括对快速应用程序重新启动的支持。有关详细信息，请参阅[热插拔“操作方法”](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.hotswapping)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/6.-shi-yong-spring-boot-jin-xing-kai-fa/6.7.-yun-xing-nin-de-ying-yong-cheng-xu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
