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

Whenever a user enters "~" as an argument, my program replaces it with System.getProperty("user.home").

After debugging, I see that this replaces "~" with "C:UsersSoulBeaver" and not "C:/Users/SoulBeaver".

Going through previous questions about incorrect user.home folders, I found out that Java tries to fetch the path from

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

However, I'm using Windows 8 and there is seemingly nothing wrong:

At this point I'm assuming Java "eats" the backslash... so how do I prevent that from happening?

Update

Since the code was requested, here it is. This is taken from Allen Holub's Solving Java's Configuration Problem

/**
 * 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:

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

Answers


You are using regular expression based replacement. In the replacement pattern for java regexes, the '\' character is special. 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).


may be following can give some info.

Bug

Discussion


Need Your Help

TPL Task scheduled to run on UI thread vs UI Thread

task-parallel-library

What is the difference between code running in a task, scheduled to run on synchronization context of the UI thread vs code running directly on a message pump.

Get current Item from Custom Field in Sitecore 6.5

sitecore custom-fields

I am building a custom Field in sitecore 6.5. This field will generate new items based on the item in which the field resides (it uses the item's name to generate folders) and I need to reference the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.