> For the complete documentation index, see [llms.txt](https://jihoony.gitbook.io/developers-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jihoony.gitbook.io/developers-notes/developer/readme/spring/passing-jvm-options-from-gradle-bootrun.md).

# Passing JVM Options from Gradle bootRun

### 1. Overview <a href="#bd-overview" id="bd-overview"></a>

Gradle is a multi-purpose automation build tool to develop, compile, and test software packages. It supports a wide range of languages, but primarily, we use it for Java-based languages like Kotlin, Groovy, and Scala.

While working with Java, we might need to customize the JVM arguments in a Java application. As we are using Gradle to build a Java application, we can also customize the JVM arguments of the application by tuning the Gradle configuration.

In this tutorial, we’ll learn to pass the JVM arguments from the Gradle *bootRun* to a Spring Boot Java application.

### 2. Understanding *bootRun* <a href="#bd-understanding-bootrun" id="bd-understanding-bootrun"></a>

**Gradle&#x20;*****bootRun*****&#x20;is a gradle-specified task that comes with the default Spring Boot Gradle Plugin. It helps us to run the Spring Boot application from Gradle itself directly.** Executing the *bootRun* command starts our application in a development environment, which is very useful for testing and development purposes. Primarily, it is used for iterative development as it doesn’t need any separate build or deployment purposes.

**In short, it provides a simplified way to build an application in a dev environment and execute tasks related to spring boot development.**

### 3. Using *jvmArgs* in *build.gradle* File <a href="#bd-using-jvmargs-in-buildgradle-file" id="bd-using-jvmargs-in-buildgradle-file"></a>

Gradle provides a straightforward way to add JVM args to the *bootRun* command using the *build.gradle* file. To illustrate, let’s look at the command to add JVM args to a spring boot application using the *bootRun* command:

```
bootRun {
    jvmArgs([
        "-Xms256m",
        "-Xmx512m"
    ])
}
```

As we can see, the *max/min* heap of the springboot application is modified using the *jvmArgs* option. Now, let’s verify the JVM changes to the spring boot application using the *ps* command:

```
$ ps -ef | grep java | grep spring
502  7870  7254   0  8:07PM ??  0:03.89 /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java
-XX:TieredStopAtLevel=1 -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=IN 
-Duser.language=en com.example.demo.DemoApplication
```

In the above *bootRun* task, we changed the *max* and *min* heap of the Spring Boot application using the *jvmArgs* option. **This way, the JVM parameter will be attached to the Spring Boot application dynamically. Furthermore, we can also add customized properties to the&#x20;*****bootRun*****&#x20;using the&#x20;*****-D*****&#x20;option.** To demonstrate, let’s take a look at the *bootRun* task:

```
bootRun {
    jvmArgs(['-Dbaeldung=test', '-Xmx512m'])
}
```

This way, we can pass both the JVM options and custom property attributes. To illustrate, let’s verify the custom value with the *jvm* arguments:

```
$ ps -ef | grep java | grep spring
502  8423  7254   0  8:16PM ??  0:00.62 /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java 
-XX:TieredStopAtLevel=1  -Dbaeldung=test -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=IN 
-Duser.language=en com.example.demo.DemoApplication
```

In addition, we can also put these properties files into *gradle.properties* and then use them in the *build.gradle*:

```
baeldung=test
max.heap.size=512m
```

Now, we can use it in the *bootRun* command:

```
bootRun {
    jvmArgs([
        "-Dbaeldung=${project.findProperty('baeldung')}",
	"-Xmx${project.findProperty('max.heap.size')}"
    ])
}
```

Using the above way we can separate the configuration file from the main *build.gradle* file.

### 4. Using Command-Line Arguments <a href="#bd-using-command-line-arguments" id="bd-using-command-line-arguments"></a>

We can also provide JVM options directly to the .*/gradlew bootRun* command. In Gradle, system properties can be specified with the *-D* flag, and JVM options can be specified using *-X*:

```
$ ./gradlew bootRun --args='--spring-boot.run.jvmArguments="-Xmx512m" --baeldung=test'
```

**We can use this command to supply JVM options dynamically at runtime without modifying the Gradle build file.** To demonstrate, let’s verify the JVM arguments using the *ps* command:

```
$ ps -ef | grep java | grep spring 
 502 58504 90399   0  7:21AM ?? 0:02.95 /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java 
 -XX:TieredStopAtLevel=1 -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=IN -Duser.language=en 
 com.example.demo.DemoApplication --spring-boot.run.jvmArguments=-Xmx512m --baeldung=test
```

The above command directly sets the *jvm* arguments using the *./gradlew bootRun* command.

### 5. Conclusion <a href="#bd-conclusion" id="bd-conclusion"></a>

In this article, we learned different ways to pass the JVM options to the *bootRun* command.

First, we learned the importance and basic usage of *bootRun*. We then explored the use of command line arguments and the *build.gradle* file to supply the JVM options to *bootRun*.

{% embed url="<https://www.baeldung.com/java-gradle-bootrun-pass-jvm-options>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://jihoony.gitbook.io/developers-notes/developer/readme/spring/passing-jvm-options-from-gradle-bootrun.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
