Java System.getProperty("key") 从哪里(哪个属性文件)读取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18396535/
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
Where (which properties file) does System.getProperty("key") reads from?
提问by Rajesh Kumar
My application uses String str = System.getProperty("key","default");
which always returns default because i am not able to set the key-value pair in the properties file.
我的应用程序使用String str = System.getProperty("key","default");
which 总是返回默认值,因为我无法在属性文件中设置键值对。
I tried setting it in deployment.properties file located in users->appdata->locallow->sun->java>deployment and also directly putting key=value in runtime parameter in java control panel but not working.
我尝试在位于 users->appdata->locallow->sun->java>deployment 的 deployment.properties 文件中设置它,并直接将 key=value 放在 java 控制面板的运行时参数中,但不起作用。
Please help me to set it correctly or if there exist a different properties file where these values is to be set, kindly share the path ? I google it but couldn't find.Thanks in Advance
请帮助我正确设置它,或者如果存在要设置这些值的不同属性文件,请分享路径?我用谷歌搜索但找不到。提前致谢
Edit: We use jeety server for deployment.And we have many properties file bundled with our souce code.
编辑:我们使用 jeety 服务器进行部署。我们有许多与源代码捆绑在一起的属性文件。
采纳答案by Scott Chu
If you want to setup a custom property file for System.getProperty
, this is what we here do:
如果您想为 设置自定义属性文件System.getProperty
,这就是我们在这里所做的:
- Create a base class as a base object for all the class you'll make for your web application.
- In base class, write this code
java.io.InputStream is = loader.getResourceAsStream("custom system property filename"); System.getProperties().load(is);
- 为您将为 Web 应用程序创建的所有类创建一个基类作为基对象。
- 在基类中,编写此代码
java.io.InputStream is = loader.getResourceAsStream("custom system property filename"); System.getProperties().load(is);
回答by Suresh Atta
No need to add a separate file.
无需添加单独的文件。
Use setProperties
method.
使用setProperties
方法。
To modify the existing set of system properties, use System.setProperties. This method takes a Properties object that has been initialized to contain the properties to be set. This method replaces the entire set of system properties with the new set represented by the Properties object.
要修改现有的系统属性集,请使用 System.setProperties。此方法采用已初始化以包含要设置的属性的 Properties 对象。此方法将整个系统属性集替换为由 Properties 对象表示的新集。
Warning: Changing system properties is potentially dangerous and should be done with discretion. Many system properties are not reread after start-up and are there for informational purposes. Changing some properties may have unexpected side-effects.
警告:更改系统属性具有潜在危险,应谨慎进行。许多系统属性在启动后不会重新读取,仅供参考。更改某些属性可能会产生意想不到的副作用。
If you still want to create :Example by docs
如果您仍然想创建:文档示例
回答by Rafael Winterhalter
Well, the System.getProperty(String)
returns properties that relate to the global system of a JVM. Here you can finda list of available properties.
好吧,System.getProperty(String)
返回与 JVM 的全局系统相关的属性。您可以在此处找到可用属性的列表。
If you want to load a custom file of properties, you should load this file in your own properties object of which you can find an example here. You should keep this Properties
objectseperate of the system properties. You should never just load your custom properties into the system properties. (You could do this via System.setProperties(Properties)
.) This is like defining global variables which is a sign of poor program design.
如果你想加载一个自定义的属性文件,你应该在你自己的属性对象中加载这个文件,你可以在这里找到一个例子。您应该将此Properties
对象与系统属性分开。您永远不应该只是将自定义属性加载到系统属性中。(您可以通过 来做到这一点System.setProperties(Properties)
。)这就像定义全局变量一样,这是程序设计不佳的标志。
回答by Gyro Gearless
Javas system properties are automagically set by JVM. You may add additional properties by passing -D switches to your runtime, e.g.
Java 系统属性由 JVM 自动设置。您可以通过将 -D 开关传递给您的运行时来添加其他属性,例如
java -Dkey=blue -Dhopp=topp ....
etc.
等等。
回答by Aneesh Vijendran
The Values are set using Native code in runtime. Its set inside the System.c, and a function called Java_java_lang_System_initProperties
这些值是在运行时使用本机代码设置的。它在System.c 中设置,以及一个名为Java_java_lang_System_initProperties的函数
Snippet
片段
JNIEXPORT jobject JNICALL
Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props)
{
char buf[128];
java_props_t *sprops = GetJavaProperties(env);
jmethodID putID = (*env)->GetMethodID(env,
(*env)->GetObjectClass(env, props),
"put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
jmethodID removeID = (*env)->GetMethodID(env,
(*env)->GetObjectClass(env, props),
"remove",
"(Ljava/lang/Object;)Ljava/lang/Object;");
jmethodID getPropID = (*env)->GetMethodID(env,
(*env)->GetObjectClass(env, props),
"getProperty",
"(Ljava/lang/String;)Ljava/lang/String;");
jobject ret = NULL;
jstring jVMVal = NULL;
if (sprops == NULL || putID == NULL ) return NULL;
PUTPROP(props, "java.specification.version",
JDK_MAJOR_VERSION "." JDK_MINOR_VERSION);
PUTPROP(props, "java.specification.name",
"Java Platform API Specification");
PUTPROP(props, "java.specification.vendor",
JAVA_SPECIFICATION_VENDOR);
PUTPROP(props, "java.version", RELEASE);
PUTPROP(props, "java.vendor", VENDOR);
PUTPROP(props, "java.vendor.url", VENDOR_URL);
PUTPROP(props, "java.vendor.url.bug", VENDOR_URL_BUG);
.......
.......
.......
回答by incognito nerd
theyre stored in the debug/run configs click here for screenshot
它们存储在调试/运行配置中 单击此处获取屏幕截图
you can access them like this.
你可以像这样访问它们。
System.out.println(System.getProperty("username"));
System.out.println(System.getProperty("password"));