Forums

Full Version: urlFromString does not work with relative path on windows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Adding an image with a relative path (e.g. "images/test.gif") does not work on Windows XP. A working version of IOLib.java's urlFromString() method is posted below:

Current Version:
Code:
/**
     * From a string description, attempt to generate a URL object. The string
     * may point to an Internet location (e.g., http:// or ftp:// URL),
     * a resource on the class path (resulting in a resource URL that points
     * into the current classpath), or, if the <code>includeFileSystem</code>
     * flag is true, a file on the local filesystem
     * (resulting in a file:// URL). The String will be checked in that order
     * in an attempt to resolve it to a valid URL.
     * @param location the location string for which to get a URL object
     * @param referrer the class to check for classpath resource items, the
     * location string will be resolved against the package/folder containing
     * this class
     * @param includeFileSystem indicates if the file system should be
     * included in the search to resolve the location String
     * @return a URL object, or null if the location string could not be
     * resolved
     */
    public static URL urlFromString(String location, Class referrer,
                                    boolean includeFileSystem)
    {
        URL url = null;
        if ( isUrlString(location) ) {
            // explicit URL string
            try {
                url = new URL(location);
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        } else {
            // attempt to get a URL pointing into the classpath
            if ( referrer != null )
                url = referrer.getResource(location);
            else
                url = IOLib.class.getResource(location);

            if ( url == null && !location.startsWith("/") )
                url = IOLib.class.getResource("/"+location);
            
            if ( includeFileSystem && url == null ) {
                // if still not found, check the file system
                if ( (new File(location)).exists() ) {
                    try {
                        url = new URL("file:///"+location);
                    } catch ( Exception e ) {}
                }
            }
        }
        return url;
    }

New Version:
Code:
/**
     * From a string description, attempt to generate a URL object. The string
     * may point to an Internet location (e.g., http:// or ftp:// URL),
     * a resource on the class path (resulting in a resource URL that points
     * into the current classpath), or, if the <code>includeFileSystem</code>
     * flag is true, a file on the local filesystem
     * (resulting in a file:// URL). The String will be checked in that order
     * in an attempt to resolve it to a valid URL.
     * @param location the location string for which to get a URL object
     * @param referrer the class to check for classpath resource items, the
     * location string will be resolved against the package/folder containing
     * this class
     * @param includeFileSystem indicates if the file system should be
     * included in the search to resolve the location String
     * @return a URL object, or null if the location string could not be
     * resolved
     */
    public static URL urlFromString(String location, Class referrer,
                                    boolean includeFileSystem)
    {
        URL url = null;
        if ( isUrlString(location) ) {
            // explicit URL string
            try {
                url = new URL(location);
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        } else {
            // attempt to get a URL pointing into the classpath
            if ( referrer != null )
                url = referrer.getResource(location);
            else
                url = IOLib.class.getResource(location);

            if ( url == null && !location.startsWith("/") )
                url = IOLib.class.getResource("/"+location);
            
            if ( includeFileSystem && url == null ) {
                // if still not found, check the file system
                File f = new File(location);
                if ( f.exists() ) {
                    try {
                        url = f.toURI().toURL();
                    } catch ( Exception e ) {}
                }
            }
        }
        return url;
    }

The change involving this block:
// if still not found, check the file system
File f = new File(location);
if ( f.exists() ) {
try {
url = f.toURI().toURL();
} catch ( Exception e ) {}
}
Reference URL's