In java -D what does the D stand for?

前端 未结 4 1601
陌清茗
陌清茗 2021-01-31 14:59

What does the D in

-Dproperty=value

Set a system property value.

of the java application launcher stand for? For some reason it\'s been

相关标签:
4条回答
  • 2021-01-31 15:40

    It might be for Define, cause you are defining a property

    0 讨论(0)
  • 2021-01-31 15:42

    The reason is D stands for DEFINE, because what that command switch does is defining variables.

    0 讨论(0)
  • 2021-01-31 15:51

    In C/C++ compilers the similar syntax is used to define preprocessor macros from the command line:

    #include <stdio.h>
    int main(int argc, char* argv[]) {
        printf(GREETING);
        return 0;
    }
    

    .

    gcc hello.c -DGREETING="\"Hello, world\""
    

    Java doesn't have a preprocessor, but properties defined with -D are ofter used for the similar reason - to pass some program-specific information about the current environment. The only difference is that in Java we pass them in runtime, not in compile-time:

    public class Hello {
        public static void main(String[] args) {
            System.out.println(System.getProperty("greeting"));
        }
    }
    

    .

    java -Dgreeting="Hello, world" Hello
    

    I think this similarity is the source of similar syntax.

    0 讨论(0)
  • 2021-01-31 15:53

    I've always assumed it was to define the value of a property... possibly a legacy from C compilers, which often use -D as similar to #define in code.

    EDIT: The closest I have to a source for this at the moment is some JDK 1.1 documentation which specifies the flag as:

    Redefines a property value. propertyName is the name of the property whose value you want to change and newValue is the value to change it to. [...]

    That at least contains the word "redefine" which is close to "define" :)

    0 讨论(0)
提交回复
热议问题