《jdk8u源码分析》13.SetJavaCommandLineProp

src/share/bin/java.c::SetJavaCommandLineProp

/*
 * inject the -Dsun.java.command pseudo property into the args structure
 * this pseudo property is used in the HotSpot VM to expose the
 * Java class name and arguments to the main method to the VM. The
 * HotSpot VM uses this pseudo property to store the Java class name
 * (or jar file name) and the arguments to the class's main method
 * to the instrumentation memory region. The sun.java.command pseudo
 * property is not exported by HotSpot to the Java layer.
 */
void
SetJavaCommandLineProp(char *what, int argc, char **argv)
{

    int i = 0;
    size_t len = 0;
    char* javaCommand = NULL;
    //-Dsun.java.command=${java.exe完整路径} [ ${主程序完整名} | ${jar包}] ${命令行参数}
    char* dashDstr = "-Dsun.java.command=";

    if (what == NULL) {
        /* unexpected, one of these should be set. just return without
         * setting the property
         */
        return;
    }

    /* determine the amount of memory to allocate assuming
     * the individual components will be space separated
     */
    //计算javaCommand所需内存大小
    len = JLI_StrLen(what);
    for (i = 0; i < argc; i++) {
        len += JLI_StrLen(argv[i]) + 1;
    }

    /* allocate the memory */
    javaCommand = (char*) JLI_MemAlloc(len + JLI_StrLen(dashDstr) + 1);

    /* build the -D string */
    *javaCommand = '\0';
    //拼接javaCommand:-Dsun.java.command=
    JLI_StrCat(javaCommand, dashDstr);
    //拼接javaCommand:-Dsun.java.command=C:\Tools\Java\open-jre1.8.0_202\bin\java.exe
    JLI_StrCat(javaCommand, what);

	//拼接javaCommand:-Dsun.java.command=C:\Tools\Java\open-jre1.8.0_202\bin\java.exe -jar test.jar ....
    for (i = 0; i < argc; i++) {
        /* the components of the string are space separated. In
         * the case of embedded white space, the relationship of
         * the white space separated components to their true
         * positional arguments will be ambiguous. This issue may
         * be addressed in a future release.
         */
        JLI_StrCat(javaCommand, " ");
        JLI_StrCat(javaCommand, argv[i]);
    }
	
	//添加可选参数:-Dsun.java.command=${dashDstr}${what} ${argv[0]} ${argv[1]} ${argv[2]} ...
	//@see: 10.AddApplicationOptions
    AddOption(javaCommand, NULL);
}

猜你喜欢

转载自blog.csdn.net/weixin_37477523/article/details/88364439