简体   繁体   English

将-D参数作为文件传递给Java命令行

[英]Passing -D arguments to Java command line as file

I want to pass -D arguments to a Java program, which will set those variables as System Properties. 我想将-D参数传递给Java程序,该程序将这些变量设置为系统属性。 But I have 3 to 4 -D arguments and the list can grow dynamically, so is it possible to add all those arguments into a file and pass it as arguments ? 但是我有3到4个D参数,列表可以动态增长,所以可以将所有这些参数添加到文件中并将其作为参数传递吗?

Default Method 默认方法

-Dproperty=value
Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:
            java -Dfoo="some string" SomeClass

I would like to do it as 我想这样做

variables.dat
-Dfoo="some String"
-Dbar="some string"
      ....
      ....

java -SOME_OPTION variables.dat SomeClass

Is it possible to achieve this ? 是否有可能实现这一目标? Where I dint get any help from net. 我从网上得到任何帮助。 Please help me out. 请帮帮我。

It is very simple to implement. 实现起来非常简单。 Suppose the file is 假设文件是

-Dkey1=value1
-Dkey2=value2

You have to just read this file; 你必须只读这个文件; and do in a loop do System.setProperty(key1.substring(2), value1) ; 并在循环中执行System.setProperty(key1.substring(2),value1); because that is ultimately what -D option do. 因为这最终是-D选项的作用。

No, the java executable will not read this file to parse the properties. 不, java可执行文件不会读取此文件来解析属性。 You will have to rely on your shell to do something like echoing the properties from the file as part of the arguments to the call to the JVM. 您将不得不依赖shell来执行某些操作,例如将文件中的属性作为调用JVM的参数的一部分进行回显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM