Java/JNLP:替换参数列表中的user.home

Java/JNLP:替换参数列表中的user.home,java,properties,persistence,jnlp,java-web-start,Java,Properties,Persistence,Jnlp,Java Web Start,我有一个JNLP,其中我向主应用程序提供了一些参数 我想以可移植(至少是Windows/Linux)的方式给出用户主目录的子目录作为参数 我试图查看${user.home},但我不知道JNLP是否会执行替换 编辑 明确地说:我不需要在客户机上存储或查找某些属性的方法。我需要一种通过包含用户主目录的JNLP传递参数的方法。这显然至少可以在Windows和Linux平台上运行。为了实现这个目标,我只想读取JVM设置的系统属性“user.home”。但是,我认为JavaWebStart可以在客户机上执

我有一个JNLP,其中我向主应用程序提供了一些参数

我想以可移植(至少是Windows/Linux)的方式给出用户主目录的子目录作为参数

我试图查看${user.home},但我不知道JNLP是否会执行替换

编辑

明确地说:我不需要在客户机上存储或查找某些属性的方法。我需要一种通过包含用户主目录的JNLP传递参数的方法。这显然至少可以在Windows和Linux平台上运行。为了实现这个目标,我只想读取JVM设置的系统属性“user.home”。但是,我认为JavaWebStart可以在客户机上执行一些替换

确实,所需的参数可能与用户的主目录有关,并且我的应用程序可以只加载系统属性。然而,当我不想要这种行为时,有一些用例


我只是想用一种与平台无关的方式指定用户的主目录…

在jnlp文件中,web服务器自动替换的唯一值是:

  • $$codebase—请求的URL,JNLP文件名除外
  • $$name JNLP文件的名称
  • $$context web应用程序的基本URL
  • $$site指定web服务器地址
  • $$hostname服务器的名称
只有在servlet容器中使用JNLPDownloadServlet时,才会发生这些替换

主机下载JNLP文件时不可能知道用户home参数的值

如果您部署的JNLP可以作为独立运行,并且需要将参数加载到应用程序,我建议在文件不存在的特定位置使用属性文件(应用程序可以在第一次运行时动态创建属性文件,或者您可以使用安装程序来执行此操作),并设置默认值

下面是一个详细的示例:

public class Main {
    public static void main(String ... args) throws IOException{
         new Main();
    }

    private static Properties instance;

    public static Properties getProperties(){
        return instance;
    }

    private static Properties defaultProperties(){
        // implement default properties here
        Properties props = new Properties();
        props.setProperty("myApp.property1","someDefaultValue");
        return props;
    }

    public static void createDefaultPropsFile(File propsFile,Properties props) throws IOException {
        propsFile.getParentFile().mkdirs();            
        props.store(new FileWriter(propsFile),"My App Properties");
    }


    private Main() throws IOException{
         File appDir = new File(System.getProperty("user.home"), "myApp");
         // find property file
         File propsFile = new File(appDir, "settings.properties");
         Properties appProperties = new Properties(defaultProperties());
         if(!propsFile.exists()){
             createDefaultPropsFile(propsFile,appProperties);
         } else {
             appProperties.load(new FileReader(propsFile));
         }
         // this should really be done using a singleton or something similar
         instance = appProperties; 
         System.out.println("Property"+getProperties().getProperty("myApp.property1"));        
    }
}

web服务器在jnlp文件中自动替换的唯一值是:

  • $$codebase—请求的URL,JNLP文件名除外
  • $$name JNLP文件的名称
  • $$context web应用程序的基本URL
  • $$site指定web服务器地址
  • $$hostname服务器的名称
只有在servlet容器中使用JNLPDownloadServlet时,才会发生这些替换

主机下载JNLP文件时不可能知道用户home参数的值

如果您部署的JNLP可以作为独立运行,并且需要将参数加载到应用程序,我建议在文件不存在的特定位置使用属性文件(应用程序可以在第一次运行时动态创建属性文件,或者您可以使用安装程序来执行此操作),并设置默认值

下面是一个详细的示例:

public class Main {
    public static void main(String ... args) throws IOException{
         new Main();
    }

    private static Properties instance;

    public static Properties getProperties(){
        return instance;
    }

    private static Properties defaultProperties(){
        // implement default properties here
        Properties props = new Properties();
        props.setProperty("myApp.property1","someDefaultValue");
        return props;
    }

    public static void createDefaultPropsFile(File propsFile,Properties props) throws IOException {
        propsFile.getParentFile().mkdirs();            
        props.store(new FileWriter(propsFile),"My App Properties");
    }


    private Main() throws IOException{
         File appDir = new File(System.getProperty("user.home"), "myApp");
         // find property file
         File propsFile = new File(appDir, "settings.properties");
         Properties appProperties = new Properties(defaultProperties());
         if(!propsFile.exists()){
             createDefaultPropsFile(propsFile,appProperties);
         } else {
             appProperties.load(new FileReader(propsFile));
         }
         // this should really be done using a singleton or something similar
         instance = appProperties; 
         System.out.println("Property"+getProperties().getProperty("myApp.property1"));        
    }
}

谢谢你的回答,但是我不能在用户目录上存储属性。我只希望参数列表是可移植的,这样用户的主目录总是可以找到的

我的解决方案并不完美,它只是遵循Apache的Ant方式:用系统属性中相应的值替换${myvariable}的出现

代码如下:

public class PropertySubstitutor {
    private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{(.*?)\\}");

    public static String substitute(String source) {
        Matcher matcher = VARIABLE_PATTERN.matcher(source);
        Properties properties = System.getProperties();
        StringBuffer buffer = new StringBuffer(source.length());
        while (matcher.find()) {
            matcher.appendReplacement(buffer, properties.getProperty(matcher.group(1), ""));
        }
        matcher.appendTail(buffer);
        System.out.println(buffer.toString());
        return buffer.toString();
    }

    private PropertySubstitutor() {}
}
可以将此帮助器更改为使用用户定义的属性对象


我使用慢速regexp引擎,因为我只需要在应用程序启动时解析两个或多个参数。

谢谢您的回答,但我无法在用户目录中存储属性。我只希望参数列表是可移植的,这样用户的主目录总是可以找到的

我的解决方案并不完美,它只是遵循Apache的Ant方式:用系统属性中相应的值替换${myvariable}的出现

代码如下:

public class PropertySubstitutor {
    private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\$\\{(.*?)\\}");

    public static String substitute(String source) {
        Matcher matcher = VARIABLE_PATTERN.matcher(source);
        Properties properties = System.getProperties();
        StringBuffer buffer = new StringBuffer(source.length());
        while (matcher.find()) {
            matcher.appendReplacement(buffer, properties.getProperty(matcher.group(1), ""));
        }
        matcher.appendTail(buffer);
        System.out.println(buffer.toString());
        return buffer.toString();
    }

    private PropertySubstitutor() {}
}
可以将此帮助器更改为使用用户定义的属性对象


我使用慢速regexp引擎,因为我只需要在应用程序启动时解析两个或多个参数。

如果您重定向desktop-java 7及以下版本似乎设置了user.home错误。例如,与%userprofile%不一致

如果您的桌面重定向-java 7及以下版本似乎未正确设置user.home。例如,与%userprofile%不一致

为什么需要在JNLP文件本身中进行替换。只需在代码中使用
System.getProperty(“user.home”)
,甚至
新文件(System.getProperty(“user.home”),“myAppDir”)
,因为这是一个参数:它可以是指向现有目录的任意值。但在这里,在JNLP内部,我希望它是用户目录,不管最终的机器是什么。问题仍然存在,为什么它需要是一个参数?据我所知,JNLP文件是静态的,并且原样来自web服务器。它不需要添加常规VM参数以外的参数。如果您需要加载其他属性,我建议在文件不存在的特定位置使用默认属性文件(如果需要,应用程序可以在第一次运行时动态创建属性文件)。当初始参数不表示
user.home
时,值从何而来?用户是否在文本字段中指定它?是否为他们提供了某种类型的文件选择器。。就像@RudolphEst,我很难理解这个问题。顺便说一句,看看JNLPAPI的最新版本。
PersistenceService
可以用来存储属性的路径,或者更好的是存储属性本身@AndrewThompson我没有看到那个链接。我认为JavaDoc在这里是非常误导的,谢谢你的澄清。(演示站点现在已添加书签)为什么需要在JNLP文件本身中进行替换。只需使用
System.getProperty(“user.home”)