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.
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.
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.
If you want to setup a custom property file for System.getProperty, this is what we here do:
1.Create a base class as a base object for all the class you'll make for your web application.
2.In base class, write this code
java.io.InputStream is = loader.getResourceAsStream("custom system property filename");
System.getProperties().load(is);
No need to add a separate file.
Use setProperties
method.
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.
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.
Official Docs
If you still want to create :Example by docs
Well, the System.getProperty(String)
returns properties that relate to the global system of a JVM. Here you can find a list of available properties.
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
object seperate 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.
The Values are set using Native code in runtime. Its set inside the System.c, and a function called 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);
.......
.......
.......
Javas system properties are automagically set by JVM. You may add additional properties by passing -D switches to your runtime, e.g.
java -Dkey=blue -Dhopp=topp ....
etc.
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"));