1

I've the following code snipet that doesn't work in EJB code in OC4J:

String name = getClass().getPackage().getName();
name = name.replace('.','/');
URL url = this.getClass().getClassLoader().getResource(name);
File directory = new File(url.getFile());
System.out.println("url.getFile() : " + url.getFile());

if (directory.exists())     // returns false!, why??
{
    System.out.println("directory.exists() : " + directory.exists());

}

The output is:

url.getFile() : /C:/oc4j_extended_101330/j2ee/home/applications/MyEAR/MyEJB.jar!/com/me/a/service/impl/helper/Logger

But in standalone mode, the output is:

url.getFile() : /D:/eclipse/workspace/MyEJB/build/classes/com/me/a/service/impl/helper/Logger
directory.exists() : true
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219

1 Answers1

3

It doesn't work because, as the URL indicates, it points to a location inside a JAR file, which is not an existing location of the file system: /C:/oc4j_extended_101330/j2ee/home/applications/MyEAR/MyEJB.jar!/com/me/a/service/impl/helper/Logger is not an existing directory.

You should never rely on a resource loaded from the classloader pointing to a location in the file system, because most of the time, resources are embedded inside jar files.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255