代码之家  ›  专栏  ›  技术社区  ›  talya.gendler

WebLogic-在“服务器启动”中的“参数”中使用环境变量/双引号

  •  4
  • talya.gendler  · 技术社区  · 12 年前

    我有一个管理服务器、NodeManager和一个托管服务器,都在同一台机器上。 我正试图在“服务器开始”选项卡的参数字段中输入类似的内容:

    -Dmy.property=%USERPROFILE%\someDir\someJar.jar
    

    但当托管服务器启动时,它会抛出以下异常:

    打开zip文件时出错或JAR清单丢失:%USERPROFILE%\someDir\someJar.JAR

    环境变量似乎没有被转换为其值。它只是作为纯文本传递给托管服务器。 我尝试用双引号(“)包围路径,但控制台验证输入,不允许这样做: “参数不能包含”“”“”

    即使手动编辑config.xml文件也无法工作,因为在此之后管理服务器无法启动:

    <Critical> <WebLogicServer> <BEA-000362> <Server failed. Reason: [Management:141266]Parsing failure in config.xml: java.lang
    .IllegalArgumentException: Arguments may not contain '"'.>
    

    我也试过使用%20,但没有用,它只是作为%20通过。

    我想这也许与空间的价值有关 %USERPROFILE% (即“C:\documents and settings..”),但其他环境也会发生同样的情况。指向其他没有空格的目录的变量。

    我的问题是:

    是否有任何支持的方式:

    1. 使用双引号?如果我必须引用一个名称中有空格的文件夹怎么办?

    2. 引用环境变量?如果对于分布式服务器,我不得不依赖它的值,而我事先不知道变量的值,该怎么办?

    1 回复  |  直到 12 年前
        1
  •  3
  •   Mani    12 年前

    根据评论进行编辑:

    方法1:

    1. 打开setDomainEnv.cmd并搜索 export SERVER_NAME 在Linux中或 set SERVER_NAME 在Windows中。跳到下一行(即跳过当前行和下一行)
    2. 在当前行中,插入:

      customServerList="server1,server2" #this serverList should be taken as input
      isCurrServerCustom=$(echo ${customServerList} | tr ',' '\n' | grep ${SERVER_NAME} | wc -l)
      if [ $isCurrServerCustom -gt 0 ]; then
         # add customJavaArg
         JAVA_OPTIONS="-Dmy.property=${USERPROFILE}/someDir/someJar.jar"
      fi
      
    3. 保存setDomainEnv.sh文件并重新启动服务器

    请注意,我只给出了Linux的逻辑,对于Windows,可以使用类似的逻辑,但使用批脚本语法。

    方法2:

    假设域已经安装,并且用户提供JVM参数指向的服务器列表 -Dmy.property 需要添加。Jython脚本(使用wlst.sh执行)。 WLST Reference

    用法: wlst.sh script_name props_file_location

    import os
    from java.io import File
    from java.io import FileInputStream
    
    # extract properties from properties file.
    print 'Loading input properties...'
    
    propsFile       = sys.argv[1]
    propInputStream = FileInputStream(propsFile)
    configProps     = Properties()
    configProps.load(propInputStream)
    domainDir       = configProps.get("domainDir")
    
    # serverList in properties file should be comma seperated
    serverList      = configProps.get("serverList")
    
    # The current machine's logical name as mentioned while creating the domain has to be given. Basically the machine name on which NM for current host is configured on.
    # This param may not be required as an input if the machine name is configured as same as the hostname , in which case , socket module can be imported and socket.getHostName can be used.
    currMachineName = configProps.get("machineName")
    jarDir          = os.environ("USERPROFILE")
    argToAdd        = '-Dmy.property=' + jarDir + File.separator + 'someDir' + File.separator + 'someJar.jar'
    readDomain(domainDir)
    for srvr in serverList.split(",") :
        cd('/Server/' + srvr)
        listenAddr = get('ListenAddress')
        if listenAddr != currMachineName :
            # Only change current host's servers
            continue
        cd('/Server/' + srvr + '/ServerStart/' + srvr)
        argsOld = get('Arguments')
        if argsOld is not None :
            set('Arguments', argsOld + ' ' + argToAdd)
        else:
            set('Arguments', argToAdd)
    updateDomain()
    closeDomain()
    # now restart all affected servers (i.e serverList)
    # one way is to connect to adminserver and shutdown them and then start again
    

    脚本必须从要部署托管服务器的所有主机上运行,以便在JVM参数中具有特定于主机的值“USERPROFILE”。

    顺便说一句,在一行中回答您的问题:看起来JVM参数最终必须与文本一起提供。但是,如果作为JVM参数提供,那么看起来WLS不会转换环境变量。它给人的印象是,当它从startWebLogic.cmd(例如:使用%DOMAIN_HOME%等)完成时,它正在进行翻译,但它的shell/cmd执行器会进行翻译,然后启动JVM。