JDK-4761949 : getResource fail to send an URL in jar, with a directory
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2002-10-11
  • Updated: 2002-10-30
  • Resolved: 2002-10-30
Description

Name: gm110360			Date: 10/11/2002


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OPERATING SYSTEM VERSION : Windows NT Version 4.0




A DESCRIPTION OF THE PROBLEM :

The method getResource from the class Class run only
outside a jar file when the file is a directory.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create the class file from this source:

2. Create a directory with the name data where the class
file is.

3. execute the program(should be ok)

4. create a jar containing this class file, and the
directory

5. execute the jar file(should be ko)




EXPECTED VERSUS ACTUAL BEHAVIOR :

Outside a jar file:

url = file:/E:/bugJar/example/classes/data
url = file:/E:/bugJar/example/classes/data/
url = file:/E:/bugJar/example/classes/data//

when the data directory is inside a jar file:

url = null
url = null
url = null

notice that with a file(ie data is a file), it's ok.

Conclusion: impossible to test if a directory exists in a
jar file.




REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.net.URL;

public class bugJar
{

  public bugJar()
  {
    String dataStr = "data";
    URL url = bugJar.class.getResource(dataStr);
    System.out.println("url = "+url); // OK without jar
    dataStr = dataStr+"/";
    url = bugJar.class.getResource(dataStr);
    System.out.println("url = "+url); // OK without jar
    dataStr = dataStr+"\\";
    url = bugJar.class.getResource(dataStr);
    System.out.println("url = "+url); // OK without jar
  }

  public static void main(String args[])
  {
    new bugJar();
  }
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :


use the API java.util.jar to explicitly open the jar file.
(Review ID: 165652) 
======================================================================

Comments
EVALUATION There are two issues here: 1. Directory resources are always accessible through the Class.getResource() API when accessed from the filesystem. But when accessed from a jar file, it depends on whether or not the jar file was created with directory entries. If the jar file contains directory entries then Class.getResource() will return URLs that point to them. If not, then it does not return URLs. It does not seem reasonable to expect the system to invent virtual directory entries when they do not actually exist in the jar file. 2. What should a URLConnection opened on a directory actually return? In the case of the file system it returns a list of files (though this behavior is not specified and cannot be relied upon). In the case of a directory in a jar file it returns nothing because a directory entry does not contain any information (in a jar file). Again it would not be reasonable to expect the system to figure out which resources actually belong to a directory and return that list just to be compatible with the filesystem view of a class hierarchy. In general, Class.getResource() is intended to access file based resources (on the filesystem, or from jar files or wherever..) It is not specified what the effect of accessing a directory entry is, and therefore this behavior can not be expected to be the same across different URL schemes. Therefore I am going to close as not a bug. ###@###.### 2002-10-30
2002-10-30