Where (in which properties file) is System.getProperty ("key") read?

It is used in my application String str = System.getProperty("key","default");, which always returns the default value because I cannot set the key-value pair in the properties file.

I tried to install it in the deployment.properties file located in the users-> appdata-> locallow-> sun-> java> folder, and also directly put the key = value in the runtime parameter in the java control panel, but it does not work.

Please help me install it correctly or if there is another properties file in which these values ​​should be set, please share this way? I google but could not find. Thanks at Advance

Edit: we are using jeety server for deployment. And we have many property files related to our comce code.

+3
source share
5 answers

If you want to customize the custom properties file for System.getProperty, this is what we are doing here: 1. Create a base class as the base object for the entire class that you create for your web application. 2. In the base class write this code

java.io.InputStream is = loader.getResourceAsStream("custom system property filename");
System.getProperties().load(is);
+2
source

No need to add a separate file.

Use the method setProperties.

, System.setProperties. Properties, , , . , Properties.

. . . .

:

+4

, System.getProperty(String) , JVM. .

, , . Properties . . ( System.setProperties(Properties).) , .

+1

. System.c Java_java_lang_System_initProperties

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);
    .......
    .......
    .......
+1

Javas system properties are automatically set by the JVM. You can add additional properties by passing -D to switch to your work environment, for example.

java -Dkey=blue -Dhopp=topp ....

and etc.

0
source

Source: https://habr.com/ru/post/1658933/


All Articles