Java 运行jar时配置服务器端口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41694380/
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
Configure server port when running jar
提问by T code
I currently working on an Spring Boot application written in Java 8 using Gradle. What I am looking for is to pass as an argument the server port when running the Jar from the command line.
我目前正在研究使用 Gradle 用 Java 8 编写的 Spring Boot 应用程序。我正在寻找的是在从命令行运行 Jar 时将服务器端口作为参数传递。
For example:
例如:
java -jar myApplication.jar --port=8888
: This runs my Spring boot application using port8888
java -jar myApplication.jar
: Since no port number is passed as argument, the spring boot application should run on a default port number (let's say8080
)
java -jar myApplication.jar --port=8888
:这使用端口运行我的 Spring 启动应用程序8888
java -jar myApplication.jar
:由于没有端口号作为参数传递,spring boot 应用程序应该在默认端口号上运行(假设8080
)
Can anyone help me with this ?
谁能帮我这个 ?
采纳答案by Mickael
From Spring boot documentation, the command line is :
从Spring boot 文档中,命令行是:
java -Dserver.port=8888 -jar myApplication.jar
You can also use Spring boot configuration file as described in the documentation.
你也可以使用Spring引导配置文件中描述的文档。
回答by krishna Prasad
CausationAlways pass the -D<key>=<value>
JVM parameters beforethe -jar
arguments otherwise it wouldn't accept your parameters and then it will run with default values. e.g:
因果关系始终通过-D<key>=<value>
JVM参数之前的-jar
参数否则将不接受你的参数,然后它会使用默认值运行。例如:
Correct java command to execute the jar on a particular port is:
在特定端口上执行 jar 的正确 java 命令是:
java -Dserver.port=8888 -jar target/my-application-jar-path.jar
Above command will run the JVM on the port 8888but below command
上面的命令将在端口8888上运行 JVM但下面的命令
java -jar target/my-application-jar-path.jar -Dserver.port=8888
Will run on the port 8080, it will ignore the JVM parameters after -jar
会运行在8080端口,会忽略 -jar 之后的 JVM 参数
Best practice in spring-boot application it to set the server.port
into the application.properties
file as:
spring-boot 应用程序中的最佳实践server.port
将application.properties
文件设置为:
server.port=9090
Or on the particular application-<ENV>.properties
file with specific ENVIROMENT.
或者在application-<ENV>.properties
具有特定环境的特定文件上。