简体   繁体   English

Java System.getProperty(“ user.home”)目录缺少分隔符

[英]Java System.getProperty(“user.home”) directory missing separator

Whenever a user enters "~" as an argument, my program replaces it with System.getProperty("user.home"). 每当用户输入“〜”作为参数时,我的程序都会将其替换为System.getProperty(“ user.home”)。

After debugging, I see that this replaces "~" with "C:UsersSoulBeaver" and not "C:/Users/SoulBeaver". 调试后,我看到它将“〜”替换为“ C:UsersSoulBeaver”,而不是“ C:/ Users / SoulBeaver”。

Going through previous questions about incorrect user.home folders , I found out that Java tries to fetch the path from 通过前面有关不正确的user.home文件夹的问题 ,我发现Java试图从以下位置获取路径:

HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\

However, I'm using Windows 8 and there is seemingly nothing wrong: 但是,我使用的是Windows 8,似乎没有错:

At this point I'm assuming Java "eats" the backslash... so how do I prevent that from happening? 在这一点上,我假设Java“吃了”反斜杠...那么如何防止这种情况发生呢?

Update 更新

Since the code was requested, here it is. 由于请求了代码,因此就在这里。 This is taken from Allen Holub's Solving Java's Configuration Problem 这摘自艾伦·霍鲁布(Allen Holub) 解决Java的配置问题

/**
 * For every enum element in the array, treat keys[i].name() as a key
 * and load the associated value from the following places (in order):
 *
 * <ol>
 *     <li>a -D command-line switch (in System properties)</li>
 *     <li>if no -D value found, an environment variable with the same name as the key</li>
 *     <li>if no environment found, the default stored in the Enum element itself</li>
 * </ol>
 *
 * That value must identify an existing directory in the file system, and a
 * File representing that location can be retrieved from {@link #directory(Enum)}.
 *
 * @param keys The values() array associated with the enum that's using this class.
 * @throws IllegalStateException if a given key doesn't have a value associated with it
 *          or if that value doesn't identify an existing directory.
 */
public LocationsSupport(T[] keys) throws IllegalStateException {
    StringBuilder logMessage = new StringBuilder("Loaded environment/-D properties:\n");

    try {
        for (T element : keys) {
            String how = "???";
            String key = element.name();

            String value;
            if ((value = System.getProperty(key)) != null)
                how = "from system property (-D)";
            else if ((value = System.getenv(key)) != null)
                how = "from environment";
            else if ((value = element.defaultValue()) != null)
                how = "from default. Mapped from: " + value;

            if (value != null)
                value = value.replaceAll("~", System.getProperty("user.home"));

            if (value == null || value.isEmpty())
                throw new IllegalStateException("Value for " +key +" cannot be null or empty.");

            File location = new File(value);

            createLocationIfNecessary(location, element.createIfNecessary());

            if (!location.isDirectory())
                throw new IllegalStateException("Location specified in "
                        +key
                        +" (" +asString(location) +") "
                        +"does not exist or is not a directory.");


            dictionary.put(key, location);

            logMessage.append("\t");
            logMessage.append(key);
            logMessage.append("=");
            logMessage.append(asString(location) );
            logMessage.append(" (");
            logMessage.append(how);
            logMessage.append(")\n");
        }
    } finally {
        if (log.getAllAppenders() instanceof NullEnumeration)
            System.err.println(logMessage);
        else
            log.info(logMessage);
    }
}

It's failing at trying to locate the default location for CONFIG: 尝试找到CONFIG的默认位置失败:

public enum Places implements Locations {
    CONFIG ("~/config"),
    HOME   ("~"),
    TMP    ("~/tmp", true),

    TERM_STORE     ("~/tmp/indices/term_store/",     true),
    RESOURCE_STORE ("~/tmp/indices/resource_store/", true),
    PERSON_STORE   ("~/tmp/indices/person_store/",   true);

I am using Java 1.7.0_13 and IntelliJ IDEA 12.1.3 我正在使用Java 1.7.0_13IntelliJ IDEA 12.1.3

You are using regular expression based replacement. 您正在使用基于正则表达式的替换。 In the replacement pattern for java regexes, the '\\' character is special. 在Java正则表达式的替换模式中, '\\'字符是特殊字符。 you need to pass the user home dir through Matcher.quoteReplacement() before using it as a replacement pattern (as covered in the javadoc for the relevant method ). 您需要Matcher.quoteReplacement()用户主目录传递给Matcher.quoteReplacement()然后再将其用作替换模式(如相关方法javadoc所述 )。

may be following can give some info. 可能下面可以给出一些信息。

Bug 窃听器

Discussion 讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java System.getProperty(“user.home”) - Java System.getProperty(“user.home”) Java:System.getProperty(&quot;user.home&quot;) 返回“?” - Java: System.getProperty("user.home") returns "?" Java System.getProperty(“ user.home”)如何返回其他用户(pi)的主页? - Java System.getProperty(“user.home”) how return home for other user (pi)? 我如何使用根目录System.getProperty(“ user.home”) - how can i use the root directory System.getProperty(“user.home”) System.getProperty(“user.home”) 的 Inno Setup 常量等效项 - Inno Setup constant equivalent for System.getProperty(“user.home”) 通过 System.getProperty(&quot;user.home&quot;) 使用字符串作为文件位置参考 - Using Strings for File Location Reference with System.getProperty("user.home") Tomcat上的System.getProperty(“ user.home”)返回“ / usr / share / tomcat7 /” - System.getProperty(“user.home”) on Tomcat returns “/usr/share/tomcat7/” 在Tomcat上运行时,System.getProperty(“ user.home”)返回/ root - System.getProperty(“user.home”) returns /root when running on Tomcat System.getProperty(“ java.home”)的问题 - Problems with System.getProperty(“java.home”) Java System.getProperty(“line.separator”)无法在apache服务器中运行 - Java System.getProperty(“line.separator”) not working in apache server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM