This is the mail archive of the mailing list for the Java project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

URLClassLoader fix for getCanonicalFileURL fix


The last change to URLClassLoader which introduced the
getCanonicalFileURL() method contained a subtle bug. It works OK for
file URLs that point to real files, but not always for directories.
Since we later use this URL to construct a relative URL for a found
resource we must make sure that the file part ends with a separator if
it points to a directory.

2002-12-21  Mark Wielaard  <>

    * java/net/ (getCanonicalFileURL): Make sure that
    if the original file part of the URL ended with a separator that the
    canonical version also ends with a separator.

Mauve now contains tests for this case which pass with this patch.
It also makes Eclipse run much better.

OK for branch and mainline?


Index: java/net/
RCS file: /cvs/gcc/gcc/libjava/java/net/,v
retrieving revision 1.12
diff -u -r1.12
--- java/net/	11 Dec 2002 02:13:51 -0000	1.12
+++ java/net/	22 Dec 2002 21:05:14 -0000
@@ -271,14 +271,28 @@
     abstract InputStream getInputStream() throws IOException;
+  /**
+   * Returns the given URL with a canonicalized file path name when it
+   * is has the file protocol. Otherwise (or when the file part of the
+   * URL couldn't be canonicalized) it returns the original String.
+   * It makes sure that if the original file part ended with a file
+   * separator that the new file part also ends with a separator.
+   */
   static URL getCanonicalFileURL(URL url)
     if ("file".equals(url.getProtocol()))
-	    File f = new File(url.getFile()).getCanonicalFile();
-	    url = new URL("file", "", f.toString());
+	    String f = url.getFile();
+	    File file = new File(f).getCanonicalFile();
+	    String cf = file.toString();
+	    String sep = File.separator;
+	    if (f.endsWith(sep) && !cf.endsWith(sep))
+	      {
+	        cf += "/";
+	      }
+	    url = new URL("file", "", cf);
 	catch (IOException ignore)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]