将环境变量作为命令行参数传递给JAR文件。

huangapple go评论79阅读模式
英文:

pass environment variable to jar as command line arguments

问题

我正在运行一个读取代码中属性的JAR文件。现在我想在环境级别上设置一些变量,并将它们作为命令行参数传递以覆盖这些属性。我该如何做到这一点?

示例:

java -jar test.jar --server.http.host-url=https://x.com

在上述命令中,属性值server.http.host-url被设置为环境变量,名称为server.http.host.value。我如何将它作为变量传递,以便它将从环境变量中解析,就像这样:

java -jar test.jar --server.http.host-url=${server.http.host.value}
英文:

I am running a jar which reads from properties in code. Now i want to set few variables at environment level and pass them as command line arguments to override properties. How can i do that?

ex:

java -jar test.jar  --server.http.host-url=https://x.com

in the above command, property value server.http.host-url is set as env variable with name "server.http.host.value". how do i pass it as variable so it will resolved from env variables like this

java -jar test.jar  --server.http.host-url=${server.http.host.value}

答案1

得分: 4

根据您使用的 shell 不同,环境变量的名称可能会有不同的限制,但在大多数情况下,. 是不允许的,因此请将您的变量命名为类似 SERVER_HTTP_HOST_VALUE 的形式,并像这样使用它:

java -jar test.jar --server.http.host-url=$SERVER_HTTP_HOST_VALUE
英文:

Depending on the shell you are using the environment variable names might have different restrictions, but in most cases . is not allowed so name your variable something like SERVER_HTTP_HOST_VALUE for example and use it like this:

java -jar test.jar --server.http.host-url=$SERVER_HTTP_HOST_VALUE

huangapple
  • 本文由 发表于 2020年9月16日 18:06:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63917721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定