-
Bug
-
Resolution: Cannot Reproduce
-
P4
-
None
-
1.2.0
-
sparc
-
solaris_2.6
Name: ksC84122 Date: 12/17/98
According to the javadoc, the JarFile constructors should throw
FileNotFoundException if the file could not be found. The implementation,
however, throws another kind of IOException (namely ZipException)
in this case. Below please find a test example which demonstrates this
problem.
It is worth to look at the picture from a more general point of view though.
There is a number of file-opening constructors in JDK 1.2 core API.
Here's a little table to demonstrate their behaviour when a file cannot
be opened.
Specification Implementation
FileInputStream FNF FNF
FileOutputStream FNF FNF
FileReader FNF FNF
FileWriter FNF FNF
RandomAccessFile FNF FNF
ZipFile IO IO
JarFile FNF IO
It is seen that JarFile is specification-consistent with java.io
file-opening constructors and implementation-consistent with (actually
just inherited from) ZipFile constructors.
Two ways to resolve the problem look most reasonable:
1) Change JarFile specs and let it be consistent with ZipFile, i.e.
ZipFile IO IO
JarFile IO IO
2) Change ZipFile/JarFile implementation and ZipFile specs so that
they are consistent with java.io classes, i.e.
ZipFile FNF FNF
JarFile FNF FNF
There's also a third way that I personally dislike, but this one
is least dangerous from the compatibility point of view.
3) Just fix JarFile to comply with the specs, i.e.
ZipFile IO IO
JarFile FNF FNF
Please look at evaluation of 4065852 for how these inconsistencies were
resolved for java.io classes.
Here is the promised example to reproduce the JarFile problem:
===== Test11.java ========
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.jar.*;
public class Test11 {
public static void main (String argv[]) {
String fname = "nonexist.jar";
File file = null;
JarFile jf = null;
try {
file = new File( fname );
jf = new JarFile(file);
if (!file.exists())
System.out.println("FileNotFoundException expected");
return;
}
catch (FileNotFoundException fe) {
if (!file.exists()) System.out.println("OK");
else System.out.println("unexpected FileNotFoundException. File "+fname+"
exists.");
return;
}
catch (IOException e) {
e.printStackTrace();
System.out.println("unexpected IOException");
return;
} catch(SecurityException s){
SecurityManager sm = System.getSecurityManager();
if (sm!=null)
try {
sm.checkRead(fname);
} catch (SecurityException s1){
System.out.println("OKAY: Security Manager denied access to test
file");
return;
}
System.out.println("Unexpected SecurityException:" + s );
return;
}
}
}
========= Sample run (JDK-1.2fcs-R) ==========
#>java Test11
java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(Compiled Code)
at java.util.jar.JarFile.<init>(Compiled Code)
at java.util.jar.JarFile.<init>(Compiled Code)
at Test11.main(Compiled Code)
unexpected IOException
======================================================================
- relates to
-
JDK-4065852 java.io: Different file-opening constructors throw different exceptions
-
- Closed
-