Preface
In actual production environments, we often usejava -jar
Command to deploySpring Boot
project. In order to meet the needs of different scenarios, some common parameters can be used to optimize runtime behavior and performance. This article will analyze it comprehensively java -jar
The various parameters and their functions of the command help you better manage and optimizeSpring Boo
t Project deployment.
1. Basic command structure
java -jar [option] Application name.jar
The application name.jar here is the Spring Boot project JAR file you packaged, and [Options] is an optional JVM or application parameter.
2. Common java command parameters
Here are some common ones java
Command parameters and their uses.
1. Set the memory size
java -Xms512m -Xmx1024m -jar Application name.jar
- -
Xms
: Sets the minimum heap memory size allocated at the start of the JVM. For example-Xms512m
Indicates that the minimum memory is 512MB - -
Xmx
: Sets the maximum heap memory size that the JVM can use. For example-Xmx1024m
Indicates that the maximum memory is 1024MB
2. Configure the garbage collector
java -XX:+UseG1GC -jar Application name.jar
-
-XX:+UseG1GC
: Use the G1 garbage collector, suitable for scenarios where low latency is required. - Other options:
-
-XX:+UseParallelGC
: Use parallel garbage collectors, suitable for high throughput scenarios. - -
XX:+UseConcMarkSweepGC
: Use CMS (Concurrent Mark-Sweep) garbage collector, suitable for scenarios with high response time requirements.
-
3. Configure thread stack size
java -Xss512k -jar Application name.jar
- -
Xss
: Set the stack size of each thread. The default value is usually 1MB, setting a smaller value can support more threads, but may increase the risk of stack overflow.
4. Set system properties
java -=8081 -=prod -jar Application name.jar
-
-Dkey=value
: Set system properties. For example:-
: Modify the default port of Spring Boot application.
-
: Specify the running configuration file (such as
prod
Denotes the production environment).
-
5. Generate GC logs
java -Xlog:gc*:file=:time,uptime,level -jar Application name.jar
-
-Xlog:gc*
: Turn on GC logging. -
file=
: Output the log to the specified file. -
time,uptime,level
: Record timestamps, runtimes and log levels and other information.
6. Remote debugging
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar Application name.jar
-
-agentlib:jdwp
: Enable Java Debug Protocol (JDWP). -
transport=dt_socket
: Use sockets to communicate. -
server=y
: Enable the debugging server. -
suspend=n
: The application does not pause after it is started. -
address=*:5005
: Listen to debug port 5005.
3. Spring Boot special parameters
Spring Boot provides some special operating parameters that can be passed--parameter name=value
Form delivery.
1. Modify the port
java -jar application name.jar --=8081
-
--
: Modify the default running port of Spring Boot.
2. Specify the configuration file
java -jar application name.jar --=/path/to/
-
--
: Specify the path of the external configuration file.
3. Activate a Profile
java -jar application name.jar --=dev
-
--
: Activate the specified configuration file (such as dev represents the development environment).
4. Configure the log file path
java -jar application name.jar --=/path/to/
-
--
: Specify the log file location. -
--=DEBUG
: Set the global log level.
5. Configure random ports
java -jar Application name.jar --=0
-
--=0
: Let the application randomly select an available port to run.
IV. Comprehensive examples
Here is a comprehensive example showing how to run a Spring Boot application in combination with multiple parameters:
java -Xms512m -Xmx1024m -XX:+UseG1GC
-=8081 -=prod
-Xlog:gc*:file=:time,uptime,level
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
-jar Application name.jar --=/config/
5. Parameter optimization suggestions
1. Set the memory size according to the hardware resources:
Small Applications:
-Xms256m -Xmx512m
Medium-sized applications:
-Xms512m -Xmx1024m
Large-scale applications:
-Xms1g -Xmx2g
2. Choose the right garbage collector:
Response time priority: Use the G1 garbage collector.
Throughput priority: Use the Parallel garbage collector.
3. Enable remote debugging for the production environment:
However, you need to pay attention to security to avoid exposure of debug ports.
4. External configuration files:
Externalize sensitive configurations (such as database connection information) to avoid hard-code into the application.
Summarize
By rationally using various parameters of the java -jar command, the operation efficiency and maintainability of Spring Boot projects can be effectively improved. When using it in actual use, you need to adjust it according to the specific scenario and choose the most suitable configuration.