基本上,我想将以下逻辑放入install4j文件中。它将用于在应用程序中设置vmoptions。该逻辑在测试类中似乎可以正常工作,但是在install4j文件中的正确位置和正确格式中对我来说证明是相当麻烦的。我只需要将语言环境分成三个单独的vmoptions:

-Duser.language="en"
-Duser.country="US"
-Duser.variant=""


如果未提供国家/地区或变量,我只想输入一个空白字符串作为其值。
如果需要任何其他信息或代码来帮助我们完成此方案,请告诉我,如果有的话,我会提供。

用户选择的语言环境放入${installer:sys.languageId}
我尝试使用getVariable(languageId).length()获取该变量中的字符串及其长度

public class VmOptionsTest {

    public static void main(String[] args) {

        //languageOne represents ${installer:sys.languageId} in this test case
        String languageOne = "en_US";
        String language = "";
        String country = "";
        String variant = "";

        //for all the if and else if statements
        //this is where I would put getVariable(languageId).length() in install4j file
        if (languageOne.length() == 2) {

            language = languageOne;
            country = "";
            variant = "";

            JOptionPane.showMessageDialog(null, "-Duser.language=" + language + "\n" +
                    "-Duser.country=" + country + "\n" + "-Duser.variant=" + variant);
        } else if (languageOne.length() == 5) {

            language = languageOne.substring(0, 2);
            country = languageOne.substring(3, 5);
            variant = "";

            JOptionPane.showMessageDialog(null, "-Duser.language=" + language + "\n" +
                    "-Duser.country=" + country + "\n" + "-Duser.variant=" + variant);
        } else if (languageOne.length() > 5) {

            language = languageOne.substring(0, 2);
            country = languageOne.substring(3, 5);
            variant = languageOne.substring(6, 8);

            JOptionPane.showMessageDialog(null, "-Duser.language=" + language + "\n" +
                    "-Duser.country=" + country + "\n" + "-Duser.variant=" + variant);
        }


    }
}


这是上面代码的输出。

最佳答案

好吧,我终于想通了。

在我的install4j IDE中,我创建了3个新的安装程序变量。

${installer:language}
${installer:country}
${installer:variant}


然后我为每个脚本编写了一个脚本。



**${installer:language}**

String language = context.getVariable("language").toString();
String languageId = context.getVariable("sys.languageId").toString();

if(languageId.length() >= 2){

  language = languageId.substring(0,2);
}

return language;




**${installer:country}**

String country = context.getVariable("country").toString();
String languageId = context.getVariable("sys.languageId").toString();

if(languageId.length() == 2){

  country = " ";

}else if(languageId.length() > 2){

  country = languageId.substring(3, 5);

}

return country;




**${installer:variant}**

String variant = context.getVariable("variant").toString();
String languageId = context.getVariable("sys.languageId").toString();

if(languageId.length() < 8){

  variant = " ";

}else{
    variant = languageId.substring(6, 8);
}

return variant;




最后,我设置VMoptions

-Duser.language=${installer:language}
-Duser.country=${installer:country}
-Duser.variant=${installer:variant}


希望这对任何遇到类似问题的人都有帮助。

07-24 20:33