JDK-4917309 : (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:class_loading
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-09-04
  • Updated: 2013-11-01
  • Resolved: 2009-09-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u18Fixed 7 b73Fixed
Related Reports
Relates :  
Description
Name: jl125535			Date: 09/04/2003


A DESCRIPTION OF THE REQUEST :
When starting a Java program, Java needs to load lots of Java classes. java.lang.ClassLoader.findBootstrapClass() and java.net.URLClassLoader.findClass() both throw ClassNotFoundExceptions for each application class or class from third party libraries to be loaded.

I modified java.lang.Throwable.java (see attached source code and method _logThrowable() ) to print information about ClassNotFoundExceptions to stdout. Compile it and put the .class file in a newly created empty directory-tree c:\jdkpatch\java\lang.

When starting SwingSet with
    java -Xbootclasspath/p:c:\jdkpatch -jar SwingSet2.jar
285 instances of ClassNotFoundException are thrown.

In our application, consisting of thousands of classes, thousands of ClassNotFoundExceptions are thrown during startup.

JUSTIFICATION :
Throwing and catching exceptions slows down the application. In addition to that for keeping stack-traces objects need to be created and garbage-collected. This has an impact on application startup time.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I suggest using return-codes instead of exceptions wherever possible. e.g. java.lang.ClassLoader.findBootstrapClass0() could simply return null if the class was not found.

---------- BEGIN SOURCE ----------
My changes to Throwable.java:
- added new static variable "count"
- added new method "_logThrowable" (please note that the method's
  implementation is slightly different bug 4917307)
- called method "_logThrowable" from the constructor

Here is the code:

    private static int count = 0;
    private void _logThrowable()
    {
        if (getClass() == ClassNotFoundException.class)
        {
            count++;
            System.out.println("ClassNotFoundException #" + count);
            printStackTrace();
        }
    }
    public Throwable() {
        fillInStackTrace();
        _logThrowable();
    }
    public Throwable(String message) {
        fillInStackTrace();
        detailMessage = message;
        _logThrowable();
    }
    public Throwable(String message, Throwable cause) {
        fillInStackTrace();
        detailMessage = message;
        this.cause = cause;
        _logThrowable();
    }
    public Throwable(Throwable cause) {
        fillInStackTrace();
        detailMessage = (cause==null ? null : cause.toString());
        this.cause = cause;
        _logThrowable();
    }

---------- END SOURCE ----------
(Incident Review ID: 201376) 
======================================================================

Comments
SUGGESTED FIX Changeset: fdf11ce72e8e Author: mchung Date: 2009-08-06 11:25 -0700 URL: http://hg.openjdk.java.net/jdk7/tl/jdk/rev/fdf11ce72e8e 4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading Summary: Change findBootstrapClass to return null instead of throwing CNFE if class not found Reviewed-by: alanb, dholmes, iris ! src/share/classes/java/lang/ClassLoader.java ! src/share/javavm/export/jvm.h ! src/share/native/java/lang/ClassLoader.c
08-09-2009

EVALUATION ClassLoader.findBootstrapClass is an internal interface to load class with the bootstrap class loader. We can fix this internal interface to return null if class not found instead of throwing ClassNotFoundException. The public API ClassLoader.findSystemClass and ClassLoader.loadClass will throw ClassNotFoundException as they are specified. This involves the fix in the hotspot VM and JDK java.lang.ClassLoader. This CR is used to track the fix for the JDK and 6864003 is for the HS fix.
23-07-2009

EVALUATION I seem to recall a similar suggestion a while back but I haven't been able to locate the bugid. - iag@sfbay 2003-09-04
04-09-2003