I am running some code on Windows, as our customer is using Windows. For the same string version of the URL (file://sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar), URL.getFile() returns different results depending on how the URL has been created. The following code demonstrates the problem:
import java.io.*;
import java.net.*;
import java.util.jar.*;
class Test {
public static void main(String[] args) {
URL pxmlURL = ClassLoader.getSystemResource("META-INF/persistence.xml");
System.out.println(pxmlURL);
JarURLConnection conn = JarURLConnection.class.cast(pxmlURL.openConnection());
URL url = conn.getJarFileURL();
System.out.println(url); // prints file:/tmp/b.jar!/META-INF/persistence.xml
System.out.println(url.getFile()); // prints
} // main
} // Test
When I invoke 'java -cp \\sm-unwk-01.sfbay.sun.com\ss141213\issue_3209.jar;. Test', I get following output:
jar:file://sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar!/META-INF/persistence.xml
file://sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar
/ss141213/issue_3209.jar
As you can see, the UNC server name is missing in the final output. Instead of printing //sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar, it printed /ss141213/issue_3209.jar. I have tried in both JDK 5 u4 and JDK 6 u1, and got identical results. I was even more surprised when I run the following program:
class Test {
public static void main(String[] args) {
File f = new File(args[0]);
System.out.println(f.toURL());
System.out.println(f.toURL().getFile());
}
}
When I run 'java Test \\sm-unwk-01.sfbay.sun.com\ss141213\issue_3209.jar', I get
file://sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar
//sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar
The above output matches my expectation.
import java.io.*;
import java.net.*;
import java.util.jar.*;
class Test {
public static void main(String[] args) {
URL pxmlURL = ClassLoader.getSystemResource("META-INF/persistence.xml");
System.out.println(pxmlURL);
JarURLConnection conn = JarURLConnection.class.cast(pxmlURL.openConnection());
URL url = conn.getJarFileURL();
System.out.println(url); // prints file:/tmp/b.jar!/META-INF/persistence.xml
System.out.println(url.getFile()); // prints
} // main
} // Test
When I invoke 'java -cp \\sm-unwk-01.sfbay.sun.com\ss141213\issue_3209.jar;. Test', I get following output:
jar:file://sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar!/META-INF/persistence.xml
file://sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar
/ss141213/issue_3209.jar
As you can see, the UNC server name is missing in the final output. Instead of printing //sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar, it printed /ss141213/issue_3209.jar. I have tried in both JDK 5 u4 and JDK 6 u1, and got identical results. I was even more surprised when I run the following program:
class Test {
public static void main(String[] args) {
File f = new File(args[0]);
System.out.println(f.toURL());
System.out.println(f.toURL().getFile());
}
}
When I run 'java Test \\sm-unwk-01.sfbay.sun.com\ss141213\issue_3209.jar', I get
file://sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar
//sm-unwk-01.sfbay.sun.com/ss141213/issue_3209.jar
The above output matches my expectation.
- duplicates
-
JDK-6582743 File.toURL() returns wrong result for UNC pathname (win)
- Closed