1

メインアプリケーションにいくつかの引数を提供する JNLP があります。

移植可能な (少なくとも Windows/Linux) 方法で、ユーザーのホーム ディレクトリのサブディレクトリを引数として指定したいと思います。

${user.home} を調べてみましたが、JNLP が置換を実行するかどうかはよくわかりません。

編集

明確にするために: クライアント マシンでいくつかのプロパティを保存または検索する方法は必要ありません。ユーザーのホーム ディレクトリを含む JNLP を介して引数を渡す方法が必要です。これは明らかに、少なくとも Windows および Linux プラットフォームで動作するはずです。この目標を達成するには、JVM によって設定されたシステム プロパティ "user.home" を読み取りたいだけです。ただし、Java Web Start はクライアント上で何らかの置換を実行できると思います。

確かに、必要な引数はユーザーのホーム ディレクトリに相対的であり、アプリケーションはシステム プロパティをロードするだけで済みます。ただし、その動作が望ましくない場合があります。

プラットフォームに依存しない方法で、ユーザーのホーム ディレクトリを指定する方法を試してみました…</p>

4

3 に答える 3

1

The only values that are automatically substituted by the web server in jnlp files are:

  • $$codebase- The URL of the request except for the name of the JNLP file
  • $$name The name of the JNLP file
  • $$context The base URL of the web application
  • $$site The web server address
  • $$hostname The name of the server

These substitutions only happen if using the JNLPDownloadServlet in a servlet container.

The host cannot possibly know the value of the user home parameter when it downloads the JNLP file.

If you are deploying a JNLP that can be run as standalone, and you need to load arguments to the application I would recommend using a properties file at a specific location with defaults for when the file does not exist (the application can dynamically create the properties file the first time it runs or you could use an installer to do so).

Here is a detailed example:

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"));        
    }
}
于 2013-02-27T16:28:16.380 に答える
0

回答ありがとうございます。ユーザー ディレクトリにプロパティを保存できません。ユーザーのホームディレクトリが常に見つかるように、パラメーターリストを移植できるようにしたいだけです。

私の解決策は完璧ではありません。単に 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() {}
}

このヘルパーは、ユーザー定義のプロパティ オブジェクトを使用するように変更できます。

私は遅い正規表現エンジンを使用しています。これは、アプリケーションの起動時に 2 つ以上の引数を解析するだけでよいためです。

于 2013-02-28T13:02:09.967 に答える
0

デスクトップをリダイレクトする場合 - Java 7 以下では user.home が正しく設定されていないようです。たとえば、 %userprofile% とは一致しません。

于 2016-04-27T17:01:44.240 に答える