14

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.

Rajesh Kumar
  • 363
  • 2
  • 5
  • 13
  • What kind of application is it? In case of a web/enterprise application (i.e., a WAR or EAR deployment), please also mention which application server you are using. – mthmulders Aug 23 '13 at 06:50
  • @mthmulders It is basically a client server application. We use IE as our client and on the server side we deploy jetty server. – Rajesh Kumar Aug 23 '13 at 07:08
  • Take a look at the [Jetty configuration reference](http://wiki.eclipse.org/Jetty/Reference/jetty.xml_usage#Setting_Parameters_in_Configuration_Files) – mthmulders Aug 23 '13 at 07:35

6 Answers6

7

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);
    
Bashir
  • 2,057
  • 5
  • 19
  • 44
Scott Chu
  • 972
  • 14
  • 26
6

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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • The disadvantage is that this requires you to hard-code properties, while System properties are a nice (not the only) way to avoid hard-coding values in your program. – mthmulders Aug 23 '13 at 06:51
  • @SURSEH ATTA actually I need to know from where it reads(getProperty) or write(setProperty). The excat file from where these methods reading writting. – Rajesh Kumar Aug 23 '13 at 07:04
  • @RajeshKumar IMHO,[Those are resolving at run time](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/System.java#System.0props),Not from a file.When you use setproperty(),Those constants gets assigned. – Suresh Atta Aug 23 '13 at 07:55
5

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);
    .......
    .......
    .......
avijendr
  • 3,958
  • 2
  • 31
  • 46
4

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.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Hi,Actually my situation is to trace some xml instance in existing application which our customer use. Now i have to find a way to set this "key" to a value other than default so that tracing can be done. – Rajesh Kumar Aug 23 '13 at 07:23
  • You then might consider: `System.getProperties().setProperty("key", "value")`. – Rafael Winterhalter Aug 25 '13 at 14:53
1

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"));
0

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.

Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14
  • I tried it already but it's not working. Returning the default value only. Is there any other option? As far as i know -D is for boolean values. Plaese suggest – Rajesh Kumar Aug 23 '13 at 07:19
  • Yes it worked doing from command prompt but not from java control panel. – Rajesh Kumar Aug 23 '13 at 09:31